簡単なテキストだけで試しましたが、添付ファイル付きのメールを送信したいと思います。
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("Test","[email protected]"));
emailMessage.To.Add(new MailboxAddress("Demo", "[email protected]"));
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);
}
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;
詳細については、こちらをご覧ください
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加