C# 用C语言在邮件正文中附加图像#
如何在正文内容中附加图像。我已经写了下面的代码C# 用C语言在邮件正文中附加图像#,c#,image,email,smtp,C#,Image,Email,Smtp,如何在正文内容中附加图像。我已经写了下面的代码 System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); string UserName = "xyz@someorg.com"; string Password = "my password"; message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress.com")); messag
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
string UserName = "xyz@someorg.com";
string Password = "my password";
message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress.com"));
message.From = new System.Net.Mail.MailAddress("fromaddress@fromaddress.com");
message.Subject = "test subject";
message.Body = "<img src=@'C:\\Sunset.jpg'/>";
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Host = "hostname";
smtpClient.Port = 25;
smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
smtpClient.Send(message);
System.Net.Mail.MailMessage message=new System.Net.Mail.MailMessage();
字符串用户名=”xyz@someorg.com";
string Password=“我的密码”;
message.To.Add(新的System.Net.Mail.MailAddress(“toaddress@toadddress.com"));
message.From=新系统.Net.Mail.MailAddress(“fromaddress@fromaddress.com");
message.Subject=“测试主题”;
message.Body=“”;
message.IsBodyHtml=true;
System.Net.Mail.SmtpClient SmtpClient=新系统.Net.Mail.SmtpClient();
smtpClient.Host=“主机名”;
smtpClient.Port=25;
smtpClient.Credentials=新系统.Net.NetworkCredential(用户名、密码);
发送(消息);
代码很好,因为我也收到了消息,但是图像是以[X]的形式出现在身体内部,而不是图像。
如何解决这个问题?路径是否正确?使用所谓的
LinkedResource
。你可以找到方法。我们成功地做到了这一点
如果教程没有帮助,不要害羞,要求澄清。:)
参考:
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
Net系统;
使用System.IO;
使用System.Net.Mime;
使用System.Net.Mail;
命名空间是真正免费的
{
公共部分类演示_邮件:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
在这里输入代码
}
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
MailMessage Msg=新的MailMessage();
//发件人电子邮件地址。
Msg.From=新邮件地址(txtUsername.Text);
//收件人电子邮件地址。
Msg.To.Add(txtTo.Text);
Msg.Subject=txtSubject.Text;
//文件上载路径
字符串文件名=fileUpload1.PostedFile.FileName;
字符串mailbody=txtBody.Text+“
”;
//LinkedResource LinkedImage=新LinkedResource(文件名);
//HttpContext.Current.Server.MapPath(“/UploadedFiles”);
LinkedResource LinkedImage=新的LinkedResource(Server.MapPath(“~/”+文件名),“image/jpg”);
LinkedImage.ContentId=“MyPic”;
//根据Jorge的建议,为Thunderbird添加了补丁
LinkedImage.ContentType=新的ContentType(MediaTypeNames.Image.Jpeg);
AlternateView htmlView=AlternateView.CreateAlternateView-FromString(邮件正文+
" ",
null,“文本/html”);
htmlView.LinkedResources.Add(LinkedImage);
Msg.AlternateViews.Add(htmlView);
SmtpClient smtp=新SmtpClient();
smtp.Host=“smtp.gmail.com”;
smtp.Port=587;
smtp.Credentials=new System.Net.NetworkCredential(txtexerName.Text,txtpwd.Text);
smtp.EnableSsl=true;
smtp.Send(Msg);
Msg=null;
RegisterStartupScript(“UserMsg”,“alert('Mail sented thanky…');if(alert){window.location='SendMail.aspx';}”);
}
//捕获(例外情况除外)
//{
//WriteLine(“{0}捕获异常。”,ex);
//}
}
}
谢谢我解决了这条消息。Body=“”;不,你没有。收件人将该文件存储在根目录中的几率为零。@HansPassant:除非电子邮件只发送给OP!;-)哦这种方法似乎更好。对于将来使用此解决方案的任何人来说,为了使其正常工作,我必须在代码中添加以下图像标记(我的上下文是“屏幕截图”):message.Body=“”代码>
string attachmentPath = Environment.CurrentDirectory + @"\test.png";
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
message.Attachments.Add(inline);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Net.Mime;
using System.Net.Mail;
namespace ItsTrulyFree
{
public partial class demo_mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
enter code here
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
// File Upload path
String FileName = fileUpload1.PostedFile.FileName;
string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
//LinkedResource LinkedImage = new LinkedResource(FileName);
//HttpContext.Current.Server.MapPath("/UploadedFiles");
LinkedResource LinkedImage = new LinkedResource(Server.MapPath("~//" + FileName), "image/jpg");
LinkedImage.ContentId = "MyPic";
//Added the patch for Thunderbird as suggested by Jorge
LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailbody+
" <img src=cid:MyPic>",
null, "text/html");
htmlView.LinkedResources.Add(LinkedImage);
Msg.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
//catch (Exception ex)
//{
// Console.WriteLine("{0} Exception caught.", ex);
//}
}
}