我使用三个班级发送电子邮件,但我无法将文字电子邮件与图片合并,或只是发送图片。当我收到电子邮件时,我看到空图像。 帮我改变我的代码,以便我可以发送电子邮件:
和风格
public class SendService : IDistributionProvider
{
public int Send(System.Xml.Linq.XDocument recipientsData, string subject, string fromName, string fromAccount)
{
foreach (XElement element in recipientsData.Root.Elements())
{
string email = element.Element("email").Value;
string name = element.Element("name").Value;
string message = element.Element("message").Value;
bool result = EmailUtils.SendEmail(fromAccount, fromName, email, name, subject, message.Replace("\n", "<br/>"));
}
return 1;
}
public interface IDistributionProvider
{
int Send(XDocument recipientsData, string subject, string fromName,
string fromAccount);
}
public static class EmailUtils
{
private static string sendHostName;
private static int sendPort;
private static string userName;
private static string password;
private static string defaultFromEmail;
private static string defaultFromName;
static EmailUtils()
{
sendHostName = ConfigurationManager.AppSettings["sendHostName"];
sendPort = int.Parse(ConfigurationManager.AppSettings["sendPort"]);
defaultFromEmail = ConfigurationManager.AppSettings["fromEmail"];
defaultFromName = ConfigurationManager.AppSettings["fromName"];
string credential = Utils.DecryptString(ConfigurationManager.AppSettings["credential"]);
if (!string.IsNullOrEmpty(credential) && credential.Split(";".ToCharArray()).Length > 1)
{
userName = credential.Split(";".ToCharArray())[0];
password = credential.Split(";".ToCharArray())[1];
}
}
public static bool SendEmail(string toEmail, string toName, string subject, string body)
{
return SendEmail(defaultFromEmail, defaultFromName, toEmail, toName, subject, body);
}
public static bool SendEmail(string fromEmail, string fromName, string toEmail, string toName, string subject, string body)
{
try
{
if (string.IsNullOrEmpty(toEmail))
{
return false;
}
if (string.IsNullOrEmpty(toName))
{
toName = toEmail.Substring(0, toEmail.IndexOf("@"));
}
if (string.IsNullOrEmpty(fromEmail))
{
fromEmail = defaultFromEmail;
}
if (string.IsNullOrEmpty(fromName))
{
fromName = defaultFromName;
}
Message message = new Message();
message.Charset = "UTF-8";
message.Subject = Codec.RFC2047Encode(subject, "UTF-8");
message.From = new Address(fromEmail, fromName);
message.To.Add(toEmail, toName);
message.BodyHtml.Format = BodyFormat.Html;
message.BodyHtml.Charset = "UTF-8";
message.BodyHtml.Text = body;
return ActiveUp.Net.Mail.SmtpClient.SendSsl(message, sendHostName, sendPort, userName, password, SaslMechanism.Login);
}
catch
{
return false;
}
}
}
通过这种方式,我发送电子邮件 - 只是文字:
string bodyEmail = "<h2>Welcome to website</h2></br><div><p>Thank for using website</p></div>";
EmailUtils.SendEmail("xxx@gmail.com","xxxx","Contact",bodyEmail);
答案 0 :(得分:2)
最简单的方法是使用Data URIs.
内联您的图片您基本上将图片内嵌到邮件的HTML中。只需按照格式
data:[<MIME-type>][;charset=<encoding>][;base64]
其中mime-type可能是image / jpeg,charset应该是ASCII,并且图像的字节转换为base64。你可以通过从磁盘读取图像文件的字节来获得它
byte[] imaeg = File.ReadAllBytes("nekkedladies.jpg");
然后convert the byte array to a base 64 string
var base64Imaeg = System.Convert.ToBase64String(imaeg);
将它一起拍打并粘贴在你的html中(从wiki中偷来)
顺便说一下,示例图像数据不是nekkid女士。就是这样:&lt; img src =&#34; data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4 // 8 / w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg ==&#34; alt =&#34; Nekkid Ladies&#34; /&GT;
遗憾
答案 1 :(得分:0)
我实际遇到了同样的问题,真正帮助我的是this。在我的情况下,我有html,我不得不使用HtmlUtilityPack解析它。我不建议使用编码的字符串,因为它不是fully supported,它会使你的消息膨胀。 cid方式也是outlook如何将图像添加到电子邮件中。我添加了代码,但我认为这个例子在我的情况下已经足够了。