我需要发送包含图片和文字混合的邮件。我的问题是无法在身体内部发送图像,它总是出现在底层的身体上。
所以我的问题是如何在电子邮件正文中发送写位置图片?
这是代码不能正常工作
using System;
using System.Collections.Generic;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.Web;
using System.Net.Mime;
using WindowsFormsApplication5.Properties;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
string To;
To = Properties.Settings.Default.To.ToString();
msg.To.Add("some.name@gmail.com");
msg.From = new MailAddress("your.adress@gmail.com");
msg.Subject = "Subject";
string path = "C:\\root.jpg"; // my logo is placed in images folder
LinkedResource logo = new LinkedResource(path, MediaTypeNames.Image.Jpeg);
logo.ContentId = "companylogo";
logo.TransferEncoding = TransferEncoding.Base64;
//now do the HTML formatting
AlternateView av1 = AlternateView.CreateAlternateViewFromString(
"<html><body>" + "<p>This will have to be before picture</p>" + "<img src=cid:companylogo/>" + "<p>This will have to be after pictures.</p>" +
"<br></body></html>",
null, MediaTypeNames.Text.Html);
//now add the AlternateView
av1.LinkedResources.Add(logo);
//now append it to the body of the mail
msg.AlternateViews.Add(av1);
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
//for gmail
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(
"your.adress@gmail.com",
"password");
smtp.Send(msg);
}
}
}
我在gmail客户端上测试过,这就是我得到的: