问题
I've tried with only simple text but i want to sent email with attachment.
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............" };
//I want Attachment here with body text..
//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);
}
回答1:
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;
For more detail, Please visit here
来源:https://stackoverflow.com/questions/41392811/how-to-send-email-with-attachment-using-mimemessage-in-asp-net-core-web-api