tags:

views:

117

answers:

5

Hi everyone, I am having a problem while sending mails through code.Actually the code is running perfectly that there is no error,but mails are not reaching to the user whom i am sending.I am pasting my code below. Please check it and tell me the problem.

System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage(); 
msgMail.From = new System.Net.Mail.MailAddress("[email protected]");
msgMail.To.Add(new System.Net.Mail.MailAddress("[email protected]"));
string currentuseremail = web.CurrentUser.Email.ToString();
msgMail.Subject = "Request:Joing into the  myitem.Title.ToString()";
msgMail.IsBodyHtml = true;
string strBody = "test mail";
msgMail.Body = strBody;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Send(msgMail);  

and i configured web.config as:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="PickupDirectoryFromIis">
            <network host="smtpout.secureserver.net" port="25" defaultCredentials="true"/>
        </smtp>
    </mailSettings>
    <settings>
        <httpWebRequest useUnsafeHeaderParsing="true"/>
    </settings>
</system.net> 
A: 

I believe the problem in your case may be with the deliveryMethod attribute in the web.config; you may have to verify with your host, but that method will dump a file for the e-mail on the web server silently, expecting a mail transfer agent to pick up the files and send them later. The network element likely isn't being used at all, since you have the PickupDirectoryFromIis value, I think.

Andrew Barber
how to verify the deliveryMethod attribute with my host?
Abhimanyu
A: 

Surely your delivery method should be network?

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpdeliverymethod.aspx

Otherwise you'll need IIS/SMTP installed on the local machine in order for the mail to be sent out, IIRC.

Lloyd
IIS/SMTP are installed in my server and they are running.
Abhimanyu
Hmm interesting :/
Lloyd
when i changed the delivery method attribute to deliveryMethod='SpecifiedPickupDirectory'<specifiedPickupDirectory pickupDirectoryLocation="c:\maildrop" />,then i am getting the mail to that directory
Abhimanyu
A: 

Check that your local SMTP server is running. Because your DeliveryMethod is PickupDirectoryFromIis, mails will be written to the local pickup directory to be sent by the SMTP server (which is part of IIS).

liggett78
my local smtp server is running.
Abhimanyu
A: 

I believe SMTP on the machine running IIS is not configured properly or it is not running at all. You might be able to see the emails you have sent in one of the Inetpub subfolder.

Id you set PickupDirectoryFromIis, it means local SMTP server will pickup these up and relay to another SMTP.

Aliostad
A: 

Set up a local directory first to receive your mail. This will rule out any code issues before you troubleshoot any further:

http://stackoverflow.com/questions/3705908/how-to-test-asp-net-email-is-being-sent/3706075#3706075

<mailSettings>
    <smtp deliveryMethod='SpecifiedPickupDirectory'>
        <specifiedPickupDirectory pickupDirectoryLocation="c:\maildrop" />
    </smtp>
</mailSettings>

<mailSettings>
    <smtp from="[email protected]">
        <network host="xx.xx.xxx.xx" port="25" defaultCredentials="true"/>
    </smtp>
</mailSettings>
IrishChieftain
when i changed web.config to the above specified one,then the mails are dropping into that folder.
Abhimanyu
Okay, so we know it's not your code. I've updated my answer. Change your config to something like this and confirm the correct settings on your host... make sure to comment out the first mail settings for the drop folder :-)
IrishChieftain