例程:
- SmtpClient smtp = new SmtpClient();
- smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
- smtp.Host = "smtp.163.com";
- smtp.Credentials = new NetworkCredential("renzhijie1111", "**");
- MailMessage mm = new MailMessage();
- mm.From = new MailAddress("[email protected]", "test");
- mm.To.Add("[email protected]");
- mm.Subject = "測試圖片郵件";
- string plainTextBody = "如果你郵件客戶端不支持HTML格式,或者你切換到“普通文本”視圖,將看到此內容";
- mm.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain"));
- ////HTML格式郵件的內容
- string htmlBodyContent = "如果你的看到<b>這個</b>, 說明你是在以 <span style=\"color:red\">HTML</span> 格式查看郵件<br><br>";
- htmlBodyContent += "<a href=\"http://www.fenbi360.net粉筆編程網</a> <img src=\"cid:weblogo\">"; //注意此處嵌入的圖片資源
- AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");
- LinkedResource lrImage = new LinkedResource(@"d:\1.jpg", "image/gif");
- lrImage.ContentId = "weblogo"; //此處的ContentId 對應 htmlBodyContent 內容中的 cid: ,如果設置不正確,請不會顯示圖片
- htmlBody.LinkedResources.Add(lrImage);
- mm.AlternateViews.Add(htmlBody);
- ////要求回執的標誌
- mm.Headers.Add("Disposition-Notification-To", "[email protected]");
- ////自定義郵件頭
- mm.Headers.Add("X-Website", "http://www.fenbi360.net");
- ////針對 LOTUS DOMINO SERVER,插入回執頭
- mm.Headers.Add("ReturnReceipt", "1");
- mm.Priority = MailPriority.Normal; //優先級
- mm.ReplyTo = new MailAddress("[email protected]", "我自己");
- ////如果發送失敗,SMTP 服務器將發送 失敗郵件告訴我
- mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
- ////異步發送完成時的處理事件
- smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
- ////開始異步發送
- smtp.SendAsync(mm, null);
- void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e)
- {
- if (e.Cancelled)
- {
- MessageBox.Show("發送被取消");
- }
- else
- {
- if (e.Error == null)
- {
- MessageBox.Show("發送成功");
- }
- else
- {
- MessageBox.Show("發送失敗: " + e.Error.Message);
- }
- }
- }