Question

Thanks for looking at my question.

I am trying to figure out attachments for OpenNetCF.Net.Mail. Here is the code for my SendMail function:

public static void SendMessage(string subject, 
  string messageBody, 
  string fromAddress, 
  string toAddress, 
  string ccAddress)
{
    MailMessage message = new MailMessage();
    SmtpClient client = new SmtpClient();

    MailAddress address = new MailAddress(fromAddress);

    // Set the sender's address
    message.From = address;

    // Allow multiple "To" addresses to be separated by a semi-colon
    if (toAddress.Trim().Length > 0)
    {
        foreach (string addr in toAddress.Split(';'))
        {
            message.To.Add(new MailAddress(addr));
        }
    }

    // Allow multiple "Cc" addresses to be separated by a semi-colon
    if (ccAddress.Trim().Length > 0)
    {
        foreach (string addr in ccAddress.Split(';'))
        {
            message.CC.Add(new MailAddress(addr));
        }
    }

    // Set the subject and message body text
    message.Subject = subject;
    message.Body = messageBody;

    // TODO: *** Modify for your SMTP server ***
    // Set the SMTP server to be used to send the message
    client.Host = "smtp.dscarwash.com";
    string domain = "dscarwash.com";
    client.Credentials = new SmtpCredential("mailuser", "dscarwash10", domain);

    // Send the e-mail message 
    try
    {
        client.Send(message);
    }
    catch (Exception e)
    {
        string data = e.ToString();
    }
}

It is supposed to be a matter of tweaking it in the following way to allow attachments:

Attachment myAttachment = new Attachment();
message.Attachments.Add(myAttachment);

The problem is that I cannot figure out how to get the attachment to add. The lines above should be it with something else in the middle where I actually tell it what file I would like to attach. Any help on this matter will be greatly appreciated.

Thanks again!

Was it helpful?

Solution

As per this MSDN documentation, you can specify the filename of the attachment as a parameter. Thus, you can give the fullpath as the string parameter.

OTHER TIPS

They have AttachmentBase which can be used to construct a email attachment. However, we cannot add the instance of AttachmentBase to the attachments of email message.

I think the Attachment class should inherit from AttachmentBase. I think this maybe a defect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top