如何从C#.net发送电子邮件?

时间:2013-02-28 06:27:53

标签: asp.net

我正在研究框架2.我想通过我的门户网站发送电子邮件。我访问了很多网站。从而我能够理解这一点。这是我的代码。我创建了网页设计。什么是smtpserver。这个代码给我错误。

  

System.Web.HttpException:服务器拒绝了发件人地址

  MailMessage Msg = new MailMessage();
        // Sender e-mail address.
        Msg.From = txtFrom.Text;
        // Recipient e-mail address.
        Msg.To = txtTo.Text;
        Msg.Subject = txtSubject.Text;
        Msg.Body = txtBody.Text;
        // your remote SMTP server IP.
        SmtpMail.SmtpServer = "smtp.gmail.com";
        SmtpMail.Send(Msg);
        Msg = null;

3 个答案:

答案 0 :(得分:0)

如果您想使用gmail帐户发送邮件而不是使用此代码

sing System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from_add@gmail.com", "name");
var toAddress = new MailAddress("to_add@example.com", "name");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
     Host = "smtp.gmail.com",
     Port = 587,
     EnableSsl = true,
     DeliveryMethod = SmtpDeliveryMethod.Network,
     UseDefaultCredentials = false,
     Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
     {
              Subject = subject,
              Body = body
     })
{
    smtp.Send(message);
}

完全结合:How to Send Mails from your GMAIL Account through VB.NET or C#. Windows Programming, with a Bit of Customization

答案 1 :(得分:0)

试试这个以下代码

 Using System.Net.Mail;
 public void SendEmail(string from, string to, string bcc, string cc, string subject,
 string body)
 {
 try
 {  
    MailMessage NewEmail = new MailMessage();
    NewEmail.From = new MailAddress(from);
    NewEmail.To.Add(new MailAddress(to));
    if (!String.IsNullOrEmpty(bcc))
    {
        NewEmail.Bcc.Add(new MailAddress(bcc));
    }
    if (!String.IsNullOrEmpty(cc))
    {
        NewEmail.CC.Add(new MailAddress(cc));
    }
    NewEmail.Subject = subject;
    NewEmail.Body = body;
    NewEmail.IsBodyHtml = true;
    SmtpClient mSmtpClient = new SmtpClient();
    // Send the mail message
    mSmtpClient.Send(NewEmail);
    this.Label1.Text = "Email sent successful.";
}
catch
{
    this.Label1.Text = "Email sent failed";
}
}

protected void Button1_Click(object sender, EventArgs e)
{
  string from = "sender address";// like username@server.com
   string to = TextBox1.Text; //Recipient address.
   string bcc = "";
   string cc = "";
   string subject = "text";
   string body = "text";
   SendEmail(from, to, bcc, cc, subject, body); 
 }

  Web.Config:
   <system.net>
    <mailSettings>
        <smtp>
         <network host="your stmp server" port="25" userName="your from email"               password="your password"/>
       </smtp>
     </mailSettings>
     </system.net>

答案 2 :(得分:0)

在sendMail方法中传递值如下所示

 sMailFrom=your Mail Id
 sMailTo=To Mail Id
 sSubj=Subject of Mail 
 sContent= Content of ur mail
  bHTMLFormat=true

 public static string sendMail(string sMailFrom, string sMailTo, 
                   string sSubj, string   sContent, bool bHTMLFormat)

    {

        try

        {

            System.Net.Mail.MailMessage mmsg = new System.Net.Mail.MailMessage();

            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();

            //System.Web.Mail.MailMessage mmsg = new MailMessage();

            string smtpServer = ConfigurationSettings.AppSettings.Get("smtphost").ToString();

            mmsg.From = new System.Net.Mail.MailAddress(sMailFrom);
            mmsg.To.Add(sMailTo);
            mmsg.Subject = sSubj;
            mmsg.Body = sContent;

            //mmsg.BodyFormat = (bHTMLFormat ? MailFormat.Html : MailFormat.Text);
            mmsg.IsBodyHtml = bHTMLFormat;

            smtpClient.Host = smtpServer;
            if (ConfigurationSettings.AppSettings.Get("smtpport").ToString() != "")
                smtpClient.Port = Convert.ToInt32(ConfigurationSettings.AppSettings.Get("smtpport").ToString());

            smtpClient.UseDefaultCredentials = false;

            System.Net.NetworkCredential mailCredential = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings.Get("EmailID").ToString(), ConfigurationSettings.AppSettings.Get("Password").ToString());

            smtpClient.Credentials = mailCredential;
            smtpClient.EnableSsl = true;
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            smtpClient.Send(mmsg);
            return "";
        }
        catch (Exception err)
        {
            return err.Message.ToString();
        }
    }