2012-06-26 36 views

回答

3

按鈕點擊事件調用此函數

public bool SendOnlyToEmail(string sToMailAddr, string sSubject, string sMessage, 
             string sFromMailAddr) 
      { 
       try 
       { 
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 
        if (string.IsNullOrEmpty(sFromMailAddr)) 
        { 
// fetching from address from web config key 
         msg.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["MailFrom"]); 
        } 
        else 
        { 
         msg.From = new System.Net.Mail.MailAddress(sFromMailAddr); 
        } 

        foreach (string address in sToMailAddr) 
        { 
         if (address.Length > 0) 
         { 
          msg.To.Add(address); 
         } 
        } 
        msg.Subject = sSubject; 
        msg.Body = sMessage; 
        msg.IsBodyHtml = true; 

        //fetching smtp address from web config key 
        System.Net.Mail.SmtpClient objSMTPClient = new System.Net.Mail.SmtpClient(System.Configuration.ConfigurationManager.AppSettings["MailServer"]); 
        //SmtpMail.SmtpServer = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["MailServer"]); 
        if (sToMailAddr.Length > 0) 
        { 
         objSMTPClient.Send(msg); 
         return true; 
        } 
        else 
        { 
         return false; 
        } 
       } 
       catch (Exception objException) 
       { 
        ErrorLog.InsertException(objException); 
        return false; 
       } 
      } 
2

有沒有代碼唯一的方法來解決這個問題;你依賴於有一個SMTP服務器來發送你的郵件。最佳案例場景:您已經在服務器上設置了一個默認端口。在這種情況下,你需要的是這樣的:

SmtpClient client = new SmtpClient("localhost"); 
client.Send(new MailMessage("[email protected]", "[email protected]")); 

如果做不到這一點,你可以看看建立一個免費的SMTP帳戶,或(絕對必要無論如何,如果你在發出大量電子郵件規劃),得到一個與Amazon SES等電子郵件服務提供商交涉。

1

您可以使用下面的代碼來發送電子郵件:

SmtpClient smtpClient = new SmtpClient(); 
MailMessage message = new MailMessage(); 
MailAddress fromAddress = new MailAddress("senderEmail"); 
message.From = fromAddress; 
message.Subject = "your subject"; 
message.Body = txtBox.Text;//Here put the textbox text 
message.To.Add("to"); 
smtpClient.Send(message);//returns the boolean value ie. success:true 
1

<script runat="server"> 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      //create the mail message 
      MailMessage mail = new MailMessage(); 

      //set the addresses 
      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("[email protected]"); 

      //set the content 
      mail.Subject = "This is an email"; 
      mail.Body = "this is the body content of the email."; 

      //send the message 
      SmtpClient smtp = new SmtpClient(); 
      smtp.Send(mail); 
     } 
    </script>