在Asp.net中发送电子邮件c# [英] Send Email in Asp.net c#

查看:79
本文介绍了在Asp.net中发送电子邮件c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



i我正在使用asp.net c#,我想向在我网站上注册的申请人发送电子邮件,

i am using这个代码要发送,但它给出错误发送邮件失败。

 private void SendActivationEmail()
{
try
{

使用(MailMessage mm = new MailMessage(sender@sender.com,txtemail.Text))
{
mm.Subject =帐户激活;
string body =你好;
body + =Mohallah;
body + =你的密码是;
body + =< br />< br />谢谢;
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host =sender@sender.com;
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(sender@sender.com,< password>);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}





请帮助.....

解决方案

请更改代码并检查...



 MailAddress mailFrom =  new  MailAddress(GetAppSettingValue(Constants.K_APP_KEY_MAIL_FROM)); 
MailAddress mailTo = new MailAddress(mailToAdd);

使用(MailMessage message = new MailMessage(mailFrom,mailTo))
{
message.Priority = MailPriority.Normal;
message.Subject = mailSubj;
message.Body = mailBody;

// 其他用户列表不为空,请将其包含在收件人列表中。
if (!additionalToAdd.IsNull())
{
message.To.Add( new MailAddress(additionalToAdd));
}

SmtpClient client = new SmtpClient();
client.Send(message);
}





在网络配置中添加以下内容



 <   system.net  >  
< mailsettings >
< network enablessl = true host = smtp.gmail.com port = 587 用户名 = test@abc.com 密码 = 密码 / >
< smtp 来自 = 密码 deliverymethod = SpecifiedPickupDirectory >
< specifiedpickupdirectory pickupdirectorylocation = D:\00TestEmails \ / >
< / smtp >
< / mailsettings >
< / system.net >





使用上述任何一种网络或smtp。

第一个用于发送邮件..而第二个用于将邮件存储在指定的目录位置。



希望它会对你有帮助。


引用:



smtp.Host =sender@sender.com;



有你的问题 - 你正在把一个电子邮件地址放到一个属性中,该地址需要发送你电子邮件的计算机的IP地址或名称。



获取或设置主机的名称或IP地址用于SMTP事务。



将主机设置为有效的SMTP服务器。


你可以试试这个。 ..



 ///< summary> 
///发送邮件给管理员。
///< / summary>
///< param name =mailText>要发送的消息< / param>
///< returns>如果邮件发送失败,则发送邮件为真。< / returns>
private Boolean SendMail(String mailText)
{
Boolean sendMailResult;
try
{
SmtpClient smtpServer = new SmtpClient();
smtpServer.Credentials = new System.Net.NetworkCredential(mailFromId,mailFromPassword); //在此处添加电子邮件ID和密码。
smtpServer.Port = 587;
smtpServer.Host =mail.hostname.com;
smtpServer.EnableSsl = true; //不要将其用作绕过AntiVirus / Firewall的异常。
// smtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
MailMessage alertMail = new MailMessage();
alertMail.From = new MailAddress(mailFromId); //添加邮件。
alertMail.Subject =申请日报状态;
String mailId =; // recepients emailid
alertMail.To.Add(mailId);
alertMail.Body = mailText;
alertMail.IsBodyHtml = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback + =
delegate(object sender,System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true; // ****始终接受
};
smtpServer.Send(alertMail);
sendMailResult = true;
alertMail.Dispose();
smtpServer.Dispose();
返回sendMailResult;
}
catch(exception ex)
{
string mailSendingFailMessage =邮件发送失败。;
mailSendingFailMessage + = ex.GetType()。ToString()++ ex.Message;
Logger.AppendMessageToStringBuilder(mailSendingFailMessage,2);
sendMailResult = false;
返回sendMailResult;
}
最后
{

}
}


Hello,

i am using asp.net c#, i want to send email to applicants registered on my website,
i am using this code to send but it is giving error "Failure sending mail."

private void SendActivationEmail()
    {
        try
        {

            using (MailMessage mm = new MailMessage("sender@sender.com", txtemail.Text))
            {
                mm.Subject = "Account Activation";
                string body = "Hello ";
                body += "Mohallah ";
                body += "Your Password is ";
                body += "<br /><br />Thanks";
                mm.Body = body;
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "sender@sender.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential("sender@sender.com", "<password>");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }



please help .....

解决方案

Please change the code and check...

MailAddress mailFrom = new MailAddress(GetAppSettingValue(Constants.K_APP_KEY_MAIL_FROM));
               MailAddress mailTo = new MailAddress(mailToAdd);

               using (MailMessage message = new MailMessage(mailFrom, mailTo))
               {
                   message.Priority = MailPriority.Normal;
                   message.Subject = "mailSubj";
                   message.Body = "mailBody";

                   // Additional user list is not empty, include them in 'To' list.
                   if (!additionalToAdd.IsNull())
                   {
                       message.To.Add(new MailAddress(additionalToAdd));
                   }

                   SmtpClient client = new SmtpClient();
                   client.Send(message);
               }



In web config add the following

<system.net>
      <mailsettings>
          <network enablessl="true" host="smtp.gmail.com" port="587" username="test@abc.com" password="password" />
          <smtp from="password" deliverymethod="SpecifiedPickupDirectory">
            <specifiedpickupdirectory pickupdirectorylocation="D:\00TestEmails\" />
          </smtp>
      </mailsettings>  
    </system.net>



Use any of the above line either network or smtp.
The first one is used to send mail..while the 2nd is used to stored the mail in the specified directory place.

Hope it will help you.


Quote:


smtp.Host = "sender@sender.com";


There's your problem - you're putting an email address into a property which expects either the IP address or name of the computer which will send your email.


Gets or sets the name or IP address of the host used for SMTP transactions.


Set the host to a valid SMTP server.


you can try this ...

/// <summary>
        /// Send mail to admins.
        /// </summary>
        /// <param name="mailText">Message to be send</param>
        /// <returns>True if mail sent otherwise false if mail sending failed.</returns>
        private Boolean SendMail(String mailText)
        {
            Boolean sendMailResult;
            try
            {
                SmtpClient smtpServer = new SmtpClient();
                smtpServer.Credentials = new System.Net.NetworkCredential(mailFromId, mailFromPassword); //Add Email-Id and Password here.
                smtpServer.Port = 587;
                smtpServer.Host = "mail.hostname.com";
                smtpServer.EnableSsl = true; //Do not use this as to bypass exception from AntiVirus/Firewall.
                // smtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                MailMessage alertMail = new MailMessage();
                alertMail.From = new MailAddress(mailFromId); //Add Mail From.
                alertMail.Subject = "Application Daily Status";
		String mailId=""; //recepients emailid
 		alertMail.To.Add(mailId);
                alertMail.Body = mailText;
                alertMail.IsBodyHtml = true;
                System.Net.ServicePointManager.ServerCertificateValidationCallback +=
               delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                       System.Security.Cryptography.X509Certificates.X509Chain chain,
                       System.Net.Security.SslPolicyErrors sslPolicyErrors)
               {
                   return true; // **** Always accept
               };
                smtpServer.Send(alertMail);
                sendMailResult = true;
                alertMail.Dispose();
                smtpServer.Dispose();
                return sendMailResult;
            }
            catch (Exception ex)
            {
                string mailSendingFailMessage = "Mail sending failed.";
                mailSendingFailMessage += ex.GetType().ToString() + " " + ex.Message;
                Logger.AppendMessageToStringBuilder(mailSendingFailMessage, 2);
                sendMailResult = false;
                return sendMailResult;
            }
            finally
            {
               
            }
        }


这篇关于在Asp.net中发送电子邮件c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆