我如何通过asp.net从本地主机发送带有附件文件的邮件? [英] How can I send mail with a attatchment file from localhost by asp.net ?

查看:61
本文介绍了我如何通过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("anzaman_edi@yahoo.com");
             mail.To.Add("imran3may@gmail.com");
             mail.To.Add("shi01715@yahoo.com");
             //mail.To.Add("zahir@eminence-bd.org");
             mail.From = new MailAddress("shishir64092@gmail.com");
             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
                  ("shishir64092@gmail.com", "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;
         }
     }



比我得到错误消息:
找不到文件"D:\ Text.txt".

Web.Config文件中有什么问题吗?请向我发送一个web.config文件设置以解决此问题?



Than I got the error message:
Could not find file ''D:\Text.txt''.

Is there any problem in Web.Config file ? Please send me a web.config file setting for solve this problem?

推荐答案

请参阅此处:http://metalthought.blogspot.co.uk/2009/03/ssl-must-not-be-enabled-for- Pickup.html [ ^ ]


我从我的gmail本地邮件发送到Yahoo邮件时没有任何问题!
清理您的配置,因为我还没有设置任何东西,它的默认设置是确定!

但将其设置为您的配置可能对您有所帮助:
I send with out any problem from my local web app , to my yahoo mail from my gmail!
clean up your config, because I have not set any thing , its default is ok!

but set this to your config maybe helpful for you:
<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;


使用它

use it

<system.net>
    <mailsettings>
      <smtp deliverymethod="PickupDirectoryFromIis" />
    </mailsettings>
  </system.net>



它是主线:-



it is the main line:-

<smtp deliverymethod="PickupDirectoryFromIis" />

>


这篇关于我如何通过asp.net从本地主机发送带有附件文件的邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆