2013-02-28 21 views
1

我在框架2上工作。我想通過我的門戶發送電子郵件。我訪問了很多網站。從我能夠理解這一點。這裏是我的代碼。我創建了頁面設計。什麼是smtpserver.This代碼給出我錯誤。如何從C#.net發送電子郵件?

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; 
+2

已經幾百萬上SO字面上同樣的問題 - 檢查了這一點 - http://stackoverflow.com/questions/32260/sending-email在網絡通過gmail – 2013-02-28 06:31:20

+0

是你的工作? – 2013-02-28 11:31:15

回答

0
如果你想使用Gmail帳戶比使用此代碼

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

var fromAddress = new MailAddress("[email protected]", "name"); 
var toAddress = new MailAddress("[email protected]", "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); 
} 

完全soruce發送郵件

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

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 [email protected] 
    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> 
0

傳遞價值像在發方法如下

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(); 
     } 
    }