添加附件到MimeMessage [Xamarin.Android]

时间:2016-11-23 21:41:45

标签: android email xamarin mime-message

我想在我的电子邮件中添加图片附件。以下代码仅发送文本。我该怎么做才能发送图片附件?

public void SendWithSmtp(List<string> emailsToWho, string subject, string fromWho, string fromWhoEmail,
       string description, string serverName, string login, string password)
    {
        try
        {
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress(fromWho, fromWhoEmail));

            foreach (var email in emailsToWho)
            {
                message.To.Add(new MailboxAddress("Klient", email));
            }

            message.Subject = subject;
            message.Body = new TextPart("plain")
            {
                Text = description
            };

            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                //client authentication and sending
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("Email NOT sended.");
        }
    }

1 个答案:

答案 0 :(得分:1)

在组件文档中有附件的示例,试试这个

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";

// create our message text, just like before (except don't set it as the message.Body)
var body = new TextPart ("plain") {
    Text = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
"
};

// create an image attachment for the file located at path
var attachment = new MimePart ("image", "gif") {
    ContentObject = new ContentObject (File.OpenRead (path), ContentEncoding.Default),
    ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
    ContentTransferEncoding = ContentEncoding.Base64,
    FileName = Path.GetFileName (path)
};

// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new Multipart ("mixed");
multipart.Add (body);
multipart.Add (attachment);

// now set the multipart/mixed as the message body
message.Body = multipart;

更多示例请转到https://components.xamarin.com/gettingstarted/mimekit