在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、一般的邮件发送 public static void SendMail()//普通的邮件发送 { MailAddress from = new MailAddress("[email protected]"); MailAddress to = new MailAddress("[email protected]"); MailMessage message = new MailMessage(from, to); message.Subject = "邮件标题"; message.Body = "邮件内容"; MailAddress copy = new MailAddress("mailto:[email protected]"); message.CC.Add(copy); SmtpClient client = new SmtpClient("SMTP Server"); client.Credentials = CredentialCache.DefaultNetworkCredentials; client.Send(message); } 二、带HTML格式的Email发送 public static void SendMail()//带HTML的邮件发送 { MailAddress from = new MailAddress("[email protected]"); MailAddress to = new MailAddress("[email protected]"); MailMessage message = new MailMessage(from, to); message.Subject = "邮件标题"; string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容"; message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain")); string htmlBodyContent = "如果你的看到<b>这个</b>,说明你是在以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>"; htmlBodyContent += "<a href=\"http://www.baidu.com//">百度一下</a>"; AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html"); message.AlternateViews.Add(htmlBody); MailAddress copy = new MailAddress("mailto:[email protected] "); message.CC.Add(copy); SmtpClient client = new SmtpClient("SMTP Server"); client.Credentials = CredentialCache.DefaultNetworkCredentials; client.Send(message); } 三、带附件的邮件 public static void SendMail()//带HTML的邮件发送 { MailAddress from = new MailAddress("[email protected]"); MailAddress to = new MailAddress("[email protected]"); MailMessage message = new MailMessage(from, to); message.Subject = "邮件标题"; string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容"; message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain")); string htmlBodyContent = "如果你的看到<b>这个</b>,说明你是在以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>"; htmlBodyContent += "<a href=\"http://www.baidu.com//">百度一下</a>"; AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html"); message.AlternateViews.Add(htmlBody); Attachment attachment = new Attachment("C:\\附件.rar"); message.Attachments.Add(attachment); MailAddress copy = new MailAddress("mailto:[email protected]"); message.CC.Add(copy); SmtpClient client = new SmtpClient("SMTP Server"); client.Credentials = CredentialCache.DefaultNetworkCredentials; client.Send(message); } 四、邮件为HTML,而且带图片 public static void SendMail()//带HTML,并且HTML中有图片的邮件发送 { MailAddress from = new MailAddress("[email protected]"); MailAddress to = new MailAddress("[email protected]"); MailMessage message = new MailMessage(from, to); message.Subject = "邮件标题"; string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容"; message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain")); string htmlBodyContent = "如果你的看到<b>这个</b>,说明你是在以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>"; htmlBodyContent += "<a href=\"http://www.baidu.com//">百度一下</a><img src=\"cid:imageInEmail\"/>"; AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html"); LinkedResource lrImage = new LinkedResource("C:\\图片.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg); lrImage.ContentId = "imageInEmail"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,如果设置不正确,请不会显示图片 htmlBody.LinkedResources.Add(lrImage); message.AlternateViews.Add(htmlBody); MailAddress copy = new MailAddress("mailto:[email protected]"); message.CC.Add(copy); SmtpClient client = new SmtpClient("SMTP Server"); client.Credentials = CredentialCache.DefaultNetworkCredentials; client.Send(message); } 五、图片为动态生成 public static void SendMail()//带HTML,并且HTML中有图片的邮件发送 { MailAddress from = new MailAddress("[email protected]"); MailAddress to = new MailAddress("[email protected]"); MailMessage message = new MailMessage(from, to); message.Subject = "邮件标题"; string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容"; message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain")); string htmlBodyContent = "如果你的看到<b>这个</b>,说明你是在以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>"; htmlBodyContent += "<a href=\"http://www.baidu.com//">百度一下</a><img src=\"cid:imageInEmail\"/>"; AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html"); MemoryStream imgStream = GetImage(); imgStream.Position = 0;//非常重要,否则图片为空!在邮件里显示红叉 LinkedResource lrImage = new LinkedResource(imgStream, System.Net.Mime.MediaTypeNames.Image.Jpeg); lrImage.ContentId = "imageInEmail"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,如果设置不正确,请不会显示图片 htmlBody.LinkedResources.Add(lrImage); message.AlternateViews.Add(htmlBody); MailAddress copy = new MailAddress("mailto:[email protected]"); message.CC.Add(copy); SmtpClient client = new SmtpClient("SMTP Server"); client.Credentials = CredentialCache.DefaultNetworkCredentials; client.Send(message); } public static MemoryStream GetImage() { //Code here... ... } |
请发表评论