本文介绍了我如何通过asp.net从本地主机发送带有附件文件的邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码是:
My code is:
protected void Button1_Click(object sender, EventArgs e)
{
//string path = "D:\\Test.txt";
try
{
//FileStream file = null;
// //Read the file. This appears to be the offending line
//file = File.OpenRead(path);
//byte[] Content = new byte[file.Length];
//file.Read(Content, 0, (int)file.Length);
//file.Close();
//file.Dispose();
//Label1.Text = Content.ToString();
MailMessage mail = new MailMessage();
//m.ail.To.Add("[email protected]");
mail.To.Add("[email protected]");
mail.To.Add("[email protected]");
//mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]");
mail.Subject = "Send Email by asp.net code using google or gmail smtp server";
string Body = "Hi, I am testing Email function in asp.net" +
" using google or gmail smtp server"+
" and next time I will send you a document from my .aspx file";
mail.Body = Body;
Attachment at = new Attachment("D:\\Text.txt");
mail.Attachments.Add(at);
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("localhost",25);
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("[email protected]", "abcdef");
//Or your Smtp Email ID and Password
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Send(mail);
Label1.Text = "Mail Send...";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
比我得到错误消息:
Web.Config文件中有什么问题吗?请向我发送一个web.config文件设置以解决此问题?
Than I got the error message:
Is there any problem in Web.Config file ? Please send me a web.config file setting for solve this problem?
推荐答案
<system.net>
<mailSettings>
<smtp deliveryMethod="PickupDirectoryFromIis">
<network defaultCredentials="true" host="localhost" port="25"/>
</smtp>
</mailSettings>
</system.net>
并将其添加到您的代码中:
and add this to your code:
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
<system.net>
<mailsettings>
<smtp deliverymethod="PickupDirectoryFromIis" />
</mailsettings>
</system.net>
它是主线:-
it is the main line:-
<smtp deliverymethod="PickupDirectoryFromIis" />
>
这篇关于我如何通过asp.net从本地主机发送带有附件文件的邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!