我尝试过只有简单的文字,但我想发送带附件的电子邮件。
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("Test","test@gmail.com"));
emailMessage.To.Add(new MailboxAddress("Demo", "demo@gmail.com"));
emailMessage.Subject = "Hello";
emailMessage.Body = new TextPart("html") { Text = "Hi............" };
// 我希望附件带正文
//Send Email.
using (var client = new SmtpClient())
{
await client.ConnectAsync("smtp.gmail.com", 587, false);
client.AuthenticationMechanisms.Remove("XOAUTH2");
await client.AuthenticateAsync("uid", "pass");
await client.SendAsync(emailMessage);
await client.DisconnectAsync(true);
}
答案 0 :(得分:4)
var multipart = new Multipart("mixed");
multipart.Add(new TextPart("html") { Text = "your body message"});
// 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)
};
multipart.Add(attachment);
emailMessage.Body = multipart;
有关详细信息,请访问here