1

我正在研究框架 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;
4

3 回答 3

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>
于 2013-02-28T06:34:31.433 回答
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();
        }
    }
于 2013-02-28T06:47:21.807 回答
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);
}

完整来源:如何通过 VB.NET 或 C# 从您的 GMAIL 帐户发送邮件。Windows 编程,有一点自定义

于 2013-02-28T06:33:22.780 回答