如何发送电子邮件附件在Asp.Net [英] How to Send Email With Attachment In Asp.Net
本文介绍了如何发送电子邮件附件在Asp.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要与我的文件在Solution Explorer中已经添加了asp.net电子邮件附加一个图像,但我不知道如何与我的电子邮件添加此请指导我。
我目前的code以下
给出 公共无效的SendMail()
{
尝试
{
字符串receiverEmailId =name@exmp.com;
。字符串SENDERNAME = ConfigurationManager.AppSettings [从]的ToString();
。字符串的邮件服务器= ConfigurationManager.AppSettings [SMTPSERVER]的ToString(); ;
字符串senderEmailId = ConfigurationManager.AppSettings [SMTPUserName]的ToString()。
字符串密码= ConfigurationManager.AppSettings [SMTPPasssword]的ToString()。
VAR FROMADDRESS =新的MailAddress(senderEmailId,发出者);
VAR的toAddress =新的MailAddress(receiverEmailId,阿伦);
字符串主题=主题;
绳体=身上。
VAR SMTP =新SmtpClient
{
主机=smtp.gmail.com
端口= 587,
EnableSsl = TRUE,
DeliveryMethod = SmtpDeliveryMethod.Network,
证书=新的NetworkCredential(fromAddress.Address,密码)
};
使用(VAR消息= MAILMESSAGE新(FROMADDRESS,的toAddress)
{
主题=主体,
身体=人体
})
{
smtp.Send(消息);
}
}
赶上(异常前)
{
}
}
解决方案
你检查出<一个href=\"http://msdn.microsoft.com/de-de/library/system.net.mail.mailmessage.attachments.aspx\">MailMessage.Attachments财产(参见MSDN)?
//创建连接并设置媒体类型
//看到http://msdn.microsoft.com/de-de/library/system.net.mime.mediatypenames.application.aspx
附件数据=新的附件(
PATH_TO_YOUR_FILE
MediaTypeNames.Application.Octet);
//你的路径可能看起来像使用Server.Mappath(〜/ file.ABC)
message.Attachments.Add(数据);
I need to attach an image with my email in asp.net the file is already added in the solution explorer but I dont know how to add this with my email please guide me
My current code is given below
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)
{
}
}
解决方案
Did you check out MailMessage.Attachments property (see 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);
这篇关于如何发送电子邮件附件在Asp.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文