2012-06-26 35 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>