发送电子邮件、httpHandler或web服务的首选方式

本文关键字:服务 方式 web 电子邮件 httpHandler | 更新日期: 2023-09-27 18:01:06

我使用的是ASP.NET 4.0通过HttpHandlerweb service发送电子邮件的首选方式是什么?谢谢

发送电子邮件、httpHandler或web服务的首选方式

您应该在服务器端代码上发送电子邮件。如果您通过HttpHandler或web服务公开邮件api,您的系统将被垃圾邮件发送者用作中继服务器。

是的,正如@peer所建议的那样,这是个坏主意。提醒密码(一种敏感的操作(应该被安全地考虑和实现(请注意,我甚至没有提到对它们进行哈希(

我想你想要实现的是让用户知道一封电子邮件已经发送给他们,而在他们提交电子邮件后页面没有被刷新,对吗?如果是这样,您可以通过web服务将电子邮件发送到处理您的代码的服务器端代码,并将密码发送到您的电子邮件地址。然后,你可以让该函数返回true或false,让用户知道"已经发送了包含你的密码的电子邮件"或"在我们的系统中找不到这样的电子邮件">

更新

假设您有一个RemindPassword.aspx,如下所示;

<script>
$(document).ready(function () {
   $.ajax({
 type: "POST",
 data: "{'email':'" + $('#txtEmail').val() + "'}",
 url: 'RemindPassword.aspx/sendEmail',
 contentType: "application/json; charset=utf-8",
 success: function (msg) {
  isok = JSON.parse(msg.d);
      msgelem = $('#results');
      if (isok == true) {
        msgelem.html('your password has been sent to your email.')
      } else {
        msgelem.html('this email address does not exist in our system.')
      }
 }
})
   };
});
</script>
<asp:TextBox ID="txtEmail" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Send password.."/>
<span id="results"></span>

在RemindPassword.aspx中.vb:

导入System.Web.Services

<WebMethod()> _
Public Shared Function SendMail(email As String) As Boolean
    ' write code here to check if email exists.
    ' if it does, run code (or another function) to send the password.
    ' then return true
    ' if the email doesnt exist, then return false.
End Sub

您可以使用Web服务传输消息,但不会传递任何凭据。您可以从javascript调用webercice。如果你需要代码,我可以帮你。

body,cc,bcc,subject所有这些细节都可以使用javascript传递给web方法。所有其他凭据都应该保存在服务器代码中。

    function SendMail(txtTo, txtFrom, txtCC, txtBCC, txtSubject, txtMessage)
 {     

     // call server side method
     PageMethods.sendmail(txtTo, txtFrom, txtCC, txtBCC, txtSubject, txtMessage);
 }

 // set the destination textbox value with the ContactName
 function CallSuccess(res)
 {     
         alert(res) ;
 }

 // alert message on some failure
 function CallFailed(res)
 {
     alert(res.get_message());
 }

在服务器上

public static void SendMail(string txtTo, string txtFrom, string txtCC, string txtBCC, string txtSubject, string txtMessage)
{
try
    {
        //Creating the Mail Message object.
        MailMessage Email=new MailMessage();
        //Storing the To value in the object reference.
        Email.To=txtTo;                
        //Storing the From value in the object reference.
        Email.From=txtFrom;
        //Storing the CC value in the object reference.
        Email.Cc=txtCC;
        //Storing the BCC value in the object reference.
        Email.Bcc=txtBCC;
        //Storing the Subject value in the object reference.
        Email.Subject=txtSubject;
        //Specifies the email body.
        Email.Body=txtMessage;
        //Setting priority to the mail as high,low,or normal
        Email.Priority=MailPriority.High;
        //Formatting the mail as html or text.
        Email.BodyFormat=MailFormat.Text;
        //Checking whether the attachment is needed or not.
        //if(rbtnAttach)
        //{
        //    //Adding attachment to the mail.
        //    Email.Attachments.Add(
        //        new MailAttachment(FileBrowse));
        //}
        //specifying the real SMTP Mail Server.
        SmtpMail.SmtpServer.Insert(0,"127.0.0.1");
        SmtpMail.Send(Email);//Sending the mail.
        //calling the reset method to erase all the data 
        //after sending the mail.
    }
    //Catching Exception 
    catch(Exception exc)
    { 
    }
}

有关更多详细信息,您可以参考:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=109http://www.codeproject.com/KB/aspnet/Techblaster/Techblaster_demo.zip