我正在尝试通过电子邮件发送内嵌图像,它显示的很好,但我不仅收到图像,而且还收到与附件相同的图像。我如何获得它只是内嵌电子邮件,而没有单独的附件。
这是我的代码:
public async static Task SendMail(string emailAccount, string sendTo, string message, string subject, string imgpath)
{
try
{
string internalEmail = emailAccount;
string emailPassword = ConfigurationManager.AppSettings["EmailPassword"];
string displayName = "Church Musician Team";
MailMessage myMessage = new MailMessage();
myMessage.To.Add(sendTo);
myMessage.From = new MailAddress(internalEmail, displayName);
myMessage.Subject = subject;
string attachmentPath = imgpath;
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentId = "id1";
inline.ContentType.MediaType = "image/png";
myMessage.Attachments.Add(inline);
myMessage.Body = message;
myMessage.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient())
{
smtp.EnableSsl = false;
smtp.Host = ConfigurationManager.AppSettings["EmailHost"];
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(internalEmail, emailPassword);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.SendCompleted += (s, e) => { smtp.Dispose(); };
await smtp.SendMailAsync(myMessage);
}
}
catch (Exception ex)
{
throw ex;
}
}
答案 0 :(得分:2)
您可以使用以下方法将位图数据直接嵌入到正文中:
let result = {};
function recursiveFn(Obj){
for(let i in Obj){
result[mappingObj[Obj[i].id] || i]=Obj[i];
if(typeof Obj[i]==="object"){
console.log('--obj--');
recursiveFn(Obj[i]);
}else if(Object.prototype.toString.call(Obj[i]) === '[object Array]'){
console.log('--Array--');
for(var k =0 ;k<Obj[i].length;k++){
Obj[i].text = result[mappingObj[Obj[i].id] || Obj[i].text]
}
}
}
}
for(let i in Obj){
result[mappingObj[Obj[i].id] || i]=Obj[i];
if(typeof Obj[i]==="object"){
console.log('----');
recursiveFn(Obj[i])
}else if(Object.prototype.toString.call(Obj[i]) === '[object Array]'){
console.log('--Array--');
for(var k =0 ;k<Obj[i].length;k++){
Obj[i].text = result[mappingObj[Obj[i].id] || Obj[i].text]
}
}
}
console.log(JSON.stringify(result))
然后将其实际放置在体内的任何位置:
public static string BmpToUrl(Bitmap source)
{
return $"data:image/png;base64,{Convert.ToBase64String((byte[]) (new ImageConverter()).ConvertTo(source, typeof(byte[])))}";
}
这样,您无需单独的附件。
诚然,我从未真正通过电子邮件完成此操作,但我相信它应该可以工作。