0

I am using this code for sending email in asp.net:

Using System.Net.Mail

public string SendEmail()
{
    SmtpClient obj = new SmtpClient();
    MailMessage Mailmsg = new MailMessage();
    Mailmsg.To.Clear();

    Recievers = new MailAddressCollection();
    Recievers.Add(txtToAddress.Text);
    SenderName = "Info";
    SenderEmail = txtFromAddress.Text;
    Subject = "subj";
    Body = "body";
    UseBcc = false;

    if (UseBcc)
    {
        foreach (MailAddress RecieverItem in Recievers)
        {
            Mailmsg.Bcc.Add(RecieverItem);
        }
    }
    else
    {
        foreach (MailAddress RecieverItem in Recievers)
        {
            Mailmsg.To.Add(RecieverItem);
        }
    }

    Mailmsg.From = new MailAddress(SenderEmail, SenderName, System.Text.Encoding.UTF8);
    Mailmsg.Subject = Subject;
    Mailmsg.SubjectEncoding = Encoding.UTF8;
    Mailmsg.BodyEncoding = System.Text.Encoding.UTF8;
    Mailmsg.IsBodyHtml = false;

    obj.Host = mail.domain.com;

    System.Net.NetworkCredential BasicAuthenticationInfo = new System.Net.NetworkCredential("info@domain.com", "password");

    obj.UseDefaultCredentials = false;
    obj.Credentials = BasicAuthenticationInfo;

    Mailmsg.Body = Body;
    Mailmsg.IsBodyHtml = true;
    try
    {
        obj.Send(Mailmsg);
        return "sent";
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }
}

It correctly sends emails to recievers which are defined in my domain (like mail@domain.com), but I cannot send email to other mail servers (like mail@yahoo.com).

What is wrong in my code?

(May it relate to SmtpClient properties? I have set smtpclient.host to mail.mydomain.com and use username and password of one of my mail accounts which are defined in my domain)

Thanks

El Ma
  • 35
  • 3
  • 8
  • Does it throw any exception? Did you check with the SMTP server administrators (have they blocked your program on the SMTP server to prevent sending it outside?) – Sachin Nayak May 17 '12 at 05:02
  • Yes the exception is: System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: No such user here at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.SendEmail() – El Ma May 17 '12 at 05:07
  • can you check the property "FailedRecipient" on the exception SmtpFailedRecipientException. Are you sure that is a correct recipient address? – Sachin Nayak May 17 '12 at 05:14
  • the SmtpFailedRecipientException.FailedRecipient is Null ! – El Ma May 17 '12 at 05:26
  • @EIMa: make sure whether you are getting correct sender email format: txtFromAddress.Text. – Ashwini Verma May 17 '12 at 05:33
  • I fill txtFromAddress with "support@mydomain.com" or "info@mydomain.com" – El Ma May 17 '12 at 05:40
  • I would suggest that you try out that smtp server using a different email client, and talk to the smtp server administrators. the smtp server may not be knowing about any outside domains, and may be incorrectly configured – Sachin Nayak May 17 '12 at 05:58

1 Answers1

5

It must be something related to your exchange server. there are transport rules in exchange which define how you can communicate with the outside world.

http://www.msexchange.org/articles_tutorials/exchange-server-2007/management-administration/restricting-users-send-receive-external-messages-exchange-server-2007.html

you must be getting some exception while sending email to outside network

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.1.1 User unknown
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)

If this is error you are getting this mean your exchange is not supported to send email directly to outside network. as I am no MS exchange expert but i have been using a exchange server configured in my network that can't send email to outside network but we enable email forwarding to contacts.

May be this can help you. http://www.petri.co.il/configuring-exchange-2007-send-connectors.htm

also i would suggest you share this problem on https://serverfault.com/

Community
  • 1
  • 1
Shoaib Shaikh
  • 4,565
  • 1
  • 27
  • 35