开发者

Mail Sending problem

I have used this code to send mails but I am not getting any error but I'm able to receive the mail. The default smtp server is also set to "127.0.0.1" as my local host in relay mail in the "inetmgr" but I'm still not able to receive the mail. I don't know where the problem is.

In emailsender.cs class this is the code:

public void SendEmail(string To, String Subject, String Body, String uname)
{
    string body = "Hi " + uname + ",\n\n \t" + Body + "\n" + " \n Regards, \n LMS Team" + "\n\n\tSent at: " + DateTime.Now + " \n\n\t\t---- This is an a开发者_如何学编程uto generated mail. Please do not reply.";
    try
    {
        try
        {
            MailMessage Message = new MailMessage();
            Message.From = new MailAddress("karhik.varadarajan@asteor.com");

            if (!string.IsNullOrEmpty(To))
                Message.To.Add(new MailAddress(To));
            Message.Subject = Subject;
            Message.Body = body;
            try
            {
                SmtpClient smtpClient = new SmtpClient("localhost");
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                smtpClient.Port = 25;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Send(Message);
            }
            catch (System.Web.HttpException ehttp)
            {
                throw new Exception("Email Sending Failed", ehttp);
            }
        }
        catch (IndexOutOfRangeException ex)
        {
            throw new IndexOutOfRangeException("Email Sending Failed", ex);
        }
    }
    catch (System.Exception ex)
    {
        throw new Exception("Email Sending Failed", ex);
    }
}

In the .aspx file:

protected void Page_Load(object sender, EventArgs e)
{
    EmailSender email = new EmailSender();
    email.SendEmail("karhik.varadarajan@asteor.com", "testingmail", "this is a test mail", "From");
}


If you use PickupDirectoryFromIis option, Check you C:\Inetpub\mailroot\Pickup or Queue or Badmail directory whether the EML file created or not. If it is in PickUp or Queue folder, IIS may process the file. If it is in BadMail, IIS unable to process the file.


I experienced the same issue,sometimes the organization wont allow access to send email.so i tried email relaying server. try elastic email.


If there are no error there are most likely an smpt server setup problem. Firstly, you are using localhost, not 127.0.0.1. I would recommend as a best practice to use 127.0.0.1 when calling localhost.

Even if it is a "shouldn't need too" there are no reason at all, using localhost. At least put "127.0.0.1 localhost" in windows etc\hosts file. You may also try a external SMTP host that you know ou have access to (like your isp). I know misconfigured smtp hosts CAN appear as the was sended succesfully.

However, as other already stated above, there can be a lot of other problems like access to send mail. Though, i think most errors like those will throw an error back to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜