views:

711

answers:

3

Hello All,

I am using IIS 7 on Server 2008.

I just tried migrating my app from an older platform - everything works fine, except the email feature.

This is my config:

< mailSettings >   
      < smtp from="[email protected]" deliveryMethod="Network" >   
        <  network host="mail.xyz.com" port="25" userName="[email protected]" password="123"    /> < / smtp  >
    < /mailSettings >

Whenever I need to send an email I use:

 SmtpClient smtp = new SmtpClient();
 smtp.Send(email);

The funny thing is I get absolutely no errors, however the email is never sent.

The outbound firewall ruleset allows SMTP traffic.

Any idea what I did wrong?

A: 

Try this code. this too sends an email via smtp.

using System.Net;

bool bSuccess;
    //the the SMTP host.
             SmtpClient client = new SmtpClient();

             //SMTP Server
             client.Host = CommonVariables.EMAIL_SMTP_SERVER;

             //SMTP Credentials
             client.Credentials = new NetworkCredential(CommonVariables.EMAIL_USERNAME, CommonVariables.EMAIL_PASSWORD);

             //Creating a new mail.
             MailMessage mail = new MailMessage();

             //Filling 'From' Tab.
             mail.From = new MailAddress(CommonVariables.EMAIL_SENDERS_ADDRESS, CommonVariables.EMAIL_SENDERS_NAME);


                mail.To.Add("[email protected]");


             //Filling mail body.
             string szMailBody = "hhgjhgjhg";
             string szMailSubject = "sdsds";



             mail.Body = szMailBody;

             mail.Subject = szMailSubject;

             //Send Email.
             try
             {
                client.Send(mail);
                bSuccess = true;
             }
             catch (Exception Ex)
             {
                bSuccess = false;
             }

             // Clean up.
             mail.Dispose();

For this you dont need to do setting in web.config.

Ganesh R.
A: 

from this forum:

System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();

mailClient.EnableSsl = true;

also

in web.config set defaultCredentials = false;

pageman
+2  A: 

Hi, If you aren't getting any exceptions, then I would enable logging for SNM. This will at least show you if the mail was sent by SNM, and if so, then you can look elseware for the problem.

To enable logging, add the following to your .config file:

<configuration>

<sources>

  <source name="System.Net" >
    <listeners>
      <add name="MyTraceFile"/>
    </listeners>
  </source>

  <source name="System.Net.Sockets">
    <listeners>
      <add name="MyTraceFile"/>
    </listeners>
  </source>

</sources>


<sharedListeners>
  <add
    name="MyTraceFile"
    type="System.Diagnostics.TextWriterTraceListener"
    initializeData="System.Net.trace.log"                />
</sharedListeners>

<switches>
  <add name="System.Net" value="Verbose" />
  <add name="System.Net.Sockets" value="Verbose" />
</switches>

Here is an example log file:

System.Net Verbose: 0 : [0992] SmtpClient::.ctor(host=127.0.0.1)
System.Net Information: 0 : [0992] Associating SmtpClient#47606018 with SmtpTransport#5689696
System.Net Verbose: 0 : [0992] Exiting SmtpClient::.ctor()  -> SmtpClient#47606018
System.Net Verbose: 0 : [0992] SmtpClient#47606018::Send(MailMessage#5138334)
System.Net Information: 0 : [0992] SmtpClient#47606018::Send(DeliveryMethod=Network)
System.Net Information: 0 : [0992] Associating SmtpClient#47606018 with MailMessage#5138334
System.Net Information: 0 : [0992] Associating SmtpTransport#5689696 with SmtpConnection#31950948
System.Net Information: 0 : [0992] Associating SmtpConnection#31950948 with ServicePoint#34920472
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Socket() 
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Connect(1:25#16777318)
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Connect() 
System.Net Information: 0 : [0992] Associating SmtpConnection#31950948 with SmtpPooledStream#48167163
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Receive()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Receive
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 32 32 30 20 77 32 6B 20-4D 69 63 72 6F 73 6F 66 : 220 w2k Microsof
System.Net.Sockets Verbose: 0 : [0992] 00000010 : 74 20 45 53 4D 54 50 20-4D 41 49 4C 20 53 65 72 : t ESMTP MAIL Ser
System.Net.Sockets Verbose: 0 : [0992] 00000020 : 76 69 63 65 2C 20 56 65-72 73 69 6F 6E 3A 20 35 : vice, Version: 5
System.Net.Sockets Verbose: 0 : [0992] 00000030 : 2E 30 2E 32 31 39 35 2E-36 37 31 33 20 72 65 61 : .0.2195.6713 rea
System.Net.Sockets Verbose: 0 : [0992] 00000040 : 64 79 20 61 74 20 20 53-61 74 2C 20 33 31 20 44 : dy at  Sat, 31 D
System.Net.Sockets Verbose: 0 : [0992] 00000050 : 65 63 20 32 30 30 35 20-32 32 3A 31 33 3A 31 34 : ec 2005 22:13:14
System.Net.Sockets Verbose: 0 : [0992] 00000060 : 20 2D 30 36 30 30 20 0D-0A                      :  -0600 ..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Receive()   -> 105#105
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Send()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Send
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 45 48 4C 4F 20 77 32 6B-0D 0A                   : EHLO w2k..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Send()  -> 10#10
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Receive()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Receive
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 32 35 30 2D 77 32 6B 20-48 65 6C 6C 6F 20 5B 31 : 250-w2k Hello [1
System.Net.Sockets Verbose: 0 : [0992] 00000010 : 32 37 2E 30 2E 30 2E 31-5D 0D 0A 32 35 30 2D 41 : 27.0.0.1]..250-A
System.Net.Sockets Verbose: 0 : [0992] 00000020 : 55 54 48 20 47 53 53 41-50 49 20 4E 54 4C 4D 20 : UTH GSSAPI NTLM 
System.Net.Sockets Verbose: 0 : [0992] 00000030 : 4C 4F 47 49 4E 0D 0A 32-35 30 2D 41 55 54 48 3D : LOGIN..250-AUTH=
System.Net.Sockets Verbose: 0 : [0992] 00000040 : 4C 4F 47 49 4E 0D 0A 32-35 30 2D 54 55 52 4E 0D : LOGIN..250-TURN.
System.Net.Sockets Verbose: 0 : [0992] 00000050 : 0A 32 35 30 2D 41 54 52-4E 0D 0A 32 35 30 2D 53 : .250-ATRN..250-S
System.Net.Sockets Verbose: 0 : [0992] 00000060 : 49 5A 45 20 32 30 39 37-31 35 32 0D 0A 32 35 30 : IZE 2097152..250
System.Net.Sockets Verbose: 0 : [0992] 00000070 : 2D 45 54 52 4E 0D 0A 32-35 30 2D 50 49 50 45 4C : -ETRN..250-PIPEL
System.Net.Sockets Verbose: 0 : [0992] 00000080 : 49 4E 49 4E 47 0D 0A 32-35 30 2D 44 53 4E 0D 0A : INING..250-DSN..
System.Net.Sockets Verbose: 0 : [0992] 00000090 : 32 35 30 2D 45 4E 48 41-4E 43 45 44 53 54 41 54 : 250-ENHANCEDSTAT
System.Net.Sockets Verbose: 0 : [0992] 000000A0 : 55 53 43 4F 44 45 53 0D-0A 32 35 30 2D 38 62 69 : USCODES..250-8bi
System.Net.Sockets Verbose: 0 : [0992] 000000B0 : 74 6D 69 6D 65 0D 0A 32-35 30 2D 42 49 4E 41 52 : tmime..250-BINAR
System.Net.Sockets Verbose: 0 : [0992] 000000C0 : 59 4D 49 4D 45 0D 0A 32-35 30 2D 43 48 55 4E 4B : YMIME..250-CHUNK
System.Net.Sockets Verbose: 0 : [0992] 000000D0 : 49 4E 47 0D 0A 32 35 30-2D 56 52 46 59 0D 0A 32 : ING..250-VRFY..2
System.Net.Sockets Verbose: 0 : [0992] 000000E0 : 35 30 20 4F 4B 0D 0A                            : 50 OK..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Receive()   -> 231#231
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Send()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Send
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 4D 41 49 4C 20 46 52 4F-4D 3A 3C 6D 65 40 6D 79 : MAIL FROM:..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Send()  -> 30#30
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Receive()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Receive
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 32 35 30 20 32 2E 31 2E-30 20 6D 65 40 6D 79 63 : 250 2.1.0 me@myc
System.Net.Sockets Verbose: 0 : [0992] 00000010 : 6F 6D 70 61 6E 79 2E 63-6F 6D 2E 2E 2E 2E 53 65 : ompany.com....Se
System.Net.Sockets Verbose: 0 : [0992] 00000020 : 6E 64 65 72 20 4F 4B 0D-0A                      : nder OK..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Receive()   -> 41#41
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Send()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Send
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 52 43 50 54 20 54 4F 3A-3C 68 69 6D 40 68 69 73 : RCPT TO:..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Send()  -> 30#30
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Receive()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Receive
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 35 35 30 20 35 2E 37 2E-31 20 55 6E 61 62 6C 65 : 550 5.7.1 Unable
System.Net.Sockets Verbose: 0 : [0992] 00000010 : 20 74 6F 20 72 65 6C 61-79 20 66 6F 72 20 68 69 :  to relay for hi
System.Net.Sockets Verbose: 0 : [0992] 00000020 : 6D 40 68 69 73 63 6F 6D-70 61 6E 79 2E 63 6F 6D : [email protected]
System.Net.Sockets Verbose: 0 : [0992] 00000030 : 0D 0A                                           : ..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Receive()   -> 50#50
System.Net Error: 0 : [0992] Exception in the SmtpClient#47606018::Send - Mailbox unavailable. The server response was: 5.7.1 Unable to relay for [email protected]
System.Net Error: 0 : [0992]    at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Dispose()
System.Net Verbose: 0 : [0992] Exiting SmtpClient#47606018::Send()

More information can be found at my faq, here: http://systemnetmail.com/faq/4.10.aspx

99% of the time, if no exception is thrown, the email was accepted by the relay server, and it is now sitting in the relay server queue. Or, it actually did get delivered, but it was marked as spam, so it's in the recipients spam folder (or the destination server rejected it).

Cheers!

Dave

dave wanta