我需要在asp.net中附加我的电子邮件图像,该文件已添加到解决方案资源管理器中,但我不知道如何添加此电子邮件,请指导我
我目前的代码如下
public void SendMail()
{
try
{
string receiverEmailId = "name@exmp.com";
string senderName = ConfigurationManager.AppSettings["From"].ToString();
string mailServer = ConfigurationManager.AppSettings["SMTPServer"].ToString(); ;
string senderEmailId = ConfigurationManager.AppSettings["SMTPUserName"].ToString();
string password = ConfigurationManager.AppSettings["SMTPPasssword"].ToString();
var fromAddress = new MailAddress(senderEmailId, senderName);
var toAddress = new MailAddress(receiverEmailId, "Alen");
string subject = "subject";
string body = "body.";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, password)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
catch (Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud)
Pil*_*anz 24
您是否查看了MailMessage.Attachments属性(请参阅MSDN)?
// create attachment and set media Type
// see http://msdn.microsoft.com/de-de/library/system.net.mime.mediatypenames.application.aspx
Attachment data = new Attachment(
"PATH_TO_YOUR_FILE",
MediaTypeNames.Application.Octet);
// your path may look like Server.MapPath("~/file.ABC")
message.Attachments.Add(data);
Run Code Online (Sandbox Code Playgroud)
Attachment使用文件名创建类的对象,并将其添加到消息的Attachments属性中
Attachment attachment = new Attachment("file.ext");
message.Attachments.Add(attachment);
Run Code Online (Sandbox Code Playgroud)
public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)
{
try
{
// Create the mail message
MailMessage objMailMsg = new MailMessage(strFrom, strTo);
objMailMsg.BodyEncoding = Encoding.UTF8;
objMailMsg.Subject = strSubject;
objMailMsg.Body = strMsg;
Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc"));
objMailMsg.Attachments.Add(at);
objMailMsg.Priority = MailPriority.High;
objMailMsg.IsBodyHtml = true;
//prepare to send mail via SMTP transport
SmtpClient objSMTPClient = new SmtpClient();
objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
objSMTPClient.Send(objMailMsg);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60153 次 |
| 最近记录: |