65.9K
CodeProject 正在变化。 阅读更多。
Home

如何从本地机器发送带附件的电子邮件

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013 年 10 月 11 日

CPOL
viewsIcon

5536

using System.Net.Mail;public partial class SUPER_mail : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }   

using System.Net.Mail;

public partial class SUPER_mail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
      
      
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("你的 gmail 邮箱地址");
        msg.To.Add(TextBox2.Text);
        msg.Subject = TextBox3.Text;
        msg.Body = TextBox4.Text;
 if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs("C:\\Program Files (x86)\\Common Files\\microsoft shared\\DevServer\\10.0\\" + FileUpload1.FileName);
            Label3.Text = FileUpload1.FileName;
  msg.Attachments.Add(new Attachment(Label3.Text));
        }

     
        SmtpClient smt = new SmtpClient();
        smt.Send(msg);
        Label2.Text = "邮件发送成功";
    }
}

 

//在 web.config 文件中

<system.net >
    <mailSettings >
      <smtp >
        <network host ="smtp.gmail.com"  password ="你的 gmail 密码" userName ="你的 gmail 邮箱地址"/>
      </smtp>
    </mailSettings>
  </system.net>

© . All rights reserved.