在ASP.Net中发送邮件的问题 [英] Issue with Sending Mail in ASP.Net

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

问题描述

您好,我正在尝试通过我的网站发送邮件.我已使用dfollwoing代码发送邮件.但是我越来越无法发送电子邮件".任何想法……
我还从命令提示符处检查了ping我的smtp主机并对其进行了回复.

Hi, I am trying to send mail thorough my site. I have used the dfollwoing code for sending mail. But I am getting as "failed to send email". Any ides on this…
I checked pinging my smtp Host also from command prompt and its replying back.

public bool mFnSendEmail(string aStrSubject,string aStrBody)
    {
        try
        {
            MailMessage lObjMail = new MailMessage();
            lObjMail.To.Add(new MailAddress("xyz@abc.com"));
            lObjMail.From = new MailAddress("xyz@abc.com ");
            lObjMail.Subject = "Auto Generated: " + aStrSubject.Trim();
            lObjMail.Body = aStrBody;
            lObjMail.IsBodyHtml = true;
            lObjMail.Priority = MailPriority.High;
            SmtpClient lObjclient = new SmtpClient("mail.abc.COM");
                  
            try
            {
                lObjclient.Send(lObjMail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;

        }
        catch (Exception)
        {
            throw;
        }
    }


谢谢
Sreenath


Thanks
Sreenath

推荐答案

可能,您的电子邮件系统需要凭据才能发送.
这是我的通用电子邮件例程:
Probably, your email system requires credentials in order to send.
Here is my generic Email routine:
/// <summary>
/// Send an email from XXX.com
/// </summary>
/// <param name="to">Message to address</param>
/// <param name="body">Text of message to send</param>
/// <param name="subject">Subject line of message</param>
/// <param name="fromAddress">Message from address</param>
/// <param name="fromDisplay">Display neame for "message from address"</param>
/// <param name="credentialUser">User whose credentials are used for message send</param>
/// <param name="credentialPassword">User password used for message send</param>
/// <param name="attachments">Optional attachments for message</param>
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         string credentialUser,
                         string credentialPassword,
                         params MailAttachment[] attachments)
    {
    if (to == null)
        {
        to = ConfigurationManager.AppSettings["SMTPDefaultToAddress"];
        }
    string host = ConfigurationManager.AppSettings["SMTPHost"];
    body = UpgradeEmailFormat(body);
    try
        {
        MailMessage mail = new MailMessage();
        mail.Body = body;
        mail.IsBodyHtml = true;
        mail.To.Add(new MailAddress(to));
        mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
        mail.Subject = subject;
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Priority = MailPriority.Normal;
        foreach (MailAttachment ma in attachments)
            {
            mail.Attachments.Add(ma.File);
            }
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
        smtp.Host = host;
        smtp.Send(mail);
        }
    catch (Exception ex)
        {
        StringBuilder sb = new StringBuilder(1024);
        sb.Append("\nTo:" + to);
        sb.Append("\nbody:" + body);
        sb.Append("\nsubject:" + subject);
        sb.Append("\nfromAddress:" + fromAddress);
        sb.Append("\nfromDisplay:" + fromDisplay);
        sb.Append("\ncredentialUser:" + credentialUser);
        sb.Append("\ncredentialPasswordto:" + credentialPassword);
        sb.Append("\nHosting:" + host);
        ErrorLog(sb.ToString(), ex.ToString(), ErrorLogCause.EmailSystem);
        }
    }


忽略ErrorLog和UpdateEmailFormat方法:您可以删除它们或编写自己的方法.

我的web.config文件在< appSettings>中包含以下内容:部分:


Ignore the ErrorLog and UpdateEmailFormat methods: you can remove them or write your own.

My web.config file coantains the following in the <appSettings> section:

<add key="SMTPHost" value="LocalHost"/>
<add key="SMTPCredentialUser" value="DoNotReply@XXX.com"/>
<add key="SMTPCredentialPassword" value="XXXX"/>
<add key="SMTPDefaultFromAddress" value="Webmaster@XXX.com"/>
<add key="SMTPDefaultToAddress" value="Webmaster@XXX.com"/>



尝试填写您的详细信息,然后尝试-我知道它可以正常工作!



Try filling in your details, and trying that - I know it works!


原因可能有多种.您需要一一看待它们.
港口开放吗?防火墙权限到位了吗?
进一步确保已在Web.Config中配置SMTP配置:
It can be because of various reasons. You need to look at them one by one.
Is the port open? Firewall permissions in place?
Further make sure you have configured SMTP configuration in Web.Config:
<system.net>
   <mailSettings>
     <smtp from="abc@somedomain.com">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
     </smtp>
   </mailSettings>
</system.net>


如果需要,请查看此Microsoft Video教程:
使用ASP.NET发送来自网站的电子邮件 [


If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]


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

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