我正在使用ASP NET 4.0
通过HttpHandler
或web service
发送电子邮件的首选方式是什么?
感谢。
答案 0 :(得分:2)
您应该在服务器端代码上发送电子邮件。如果您通过HttpHandler或Web服务公开邮件api,您的系统将被垃圾邮件发送者用作中继服务器。
答案 1 :(得分:1)
是的,这也是一个坏主意,正如@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中:
Imports 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
答案 2 :(得分:-1)
您可以使用webservice传输邮件,但不会传递任何凭据。你可以从javascript调用websercice。如果您需要我可以帮助的代码。
主体,cc,bcc,主题所有这些细节都可以使用javascript传递给webmethod。所有其他凭据都应保存在服务器代码中。
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=109 http://www.codeproject.com/KB/aspnet/Techblaster/Techblaster_demo.zip