Link to home
Start Free TrialLog in
Avatar of awilderbeast
awilderbeastFlag for United Kingdom of Great Britain and Northern Ireland

asked on

c# send mail func trouble

hi all

my first code below works, but my second doesnt do anything can anyone help me fix it?
Thanks
############ THIS WORKS ##################
        string fromEmail = "mailout@****";//sending email from...
        string ToEmail = To.Text;
        string body = Editor1.XHTML;
        string subject = Subject.Text;

        try
        {
            SmtpClient sMail = new SmtpClient("****");//exchange or smtp server goes here.
            sMail.DeliveryMethod = SmtpDeliveryMethod.Network;
            sMail.Credentials = new NetworkCredential("mailout@*", "****");
            sMail.Send(fromEmail, ToEmail, subject, body);
        }
        catch (Exception ex)
        {
            //do something after error if there is one
        }
########### THIS DOESNT WORK #################
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(To.Text);
                mail.IsBodyHtml = true;
                mail.Body = Editor1.XHTML;
                mail.From = new MailAddress("mailout@****");
                mail.Subject = Subject.Text;

                SmtpClient sMail = new SmtpClient("*****");
                sMail.DeliveryMethod = SmtpDeliveryMethod.Network;
                sMail.Credentials = new NetworkCredential("mailout@****", "*****");
                sMail.Send(mail);
            }
            catch(Exception ex)
            {
                Editor1.Text = Convert.ToString(ex);
            }
            EditorUpdatePanel.Update();

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Can you be more specific? How does it "not work?" Are you getting an exception? Does the mail simply never arrive? Please provide more details.
Avatar of awilderbeast

ASKER

i wish i new
nothing happens at all

no exceptions no mail arrives :S

does the code look ok?
try this:
sMail.UseDefaultCredentials = false;
                sMail.DeliveryMethod = SmtpDeliveryMethod.Network;
                sMail.Credentials = new NetworkCredential("mailout@****", "*****");
                sMail.UseDefaultCredentials = false;
                sMail.Send(mail);

Open in new window

still the same, absolutley nothing is happening :S

the first code still works fine though :S
ASKER CERTIFIED SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeah yoru right, its the content of the editor!

its got a table in it, thats breaking it so far, is this a known thing that tables break it?

Thanks
this broke it too
asdasda<span style="color: red; ">sdasdas</span>

Open in new window

it says

For this to get fixed you can add CDOSYS.dll as refernce to your asp.net application and build the solution and run it should work and will not throw exception.

but where is DSOSYS.dll?

and im creating a web application if that makes differences too

Thanks
I don't know. I was just pointing you to a place where others seem to have the same difficulty.  It could be this solution presented solves it, but I've not looked into that aspect of it.  I think the issue seems somewhat sporadic and it's even possible rebooting yous system might fix it based on another entry in that same forum, I think.
i did a wireshark capture to see if the exchange server is reciveing the request and it is

so the email is getting to exchange, i dont know what the responses mean though and why there in purple?
Instead of passing the recipient via the To member, can you try passing it via the constructor like you did in the first example? I seem to recall having some issues with To, but it's been so long I can't remember what they were  : \
i dont know how to make the two bits of code work together

below is my attempt but it says no, has invalid arguments, as mail is not a string

how can i make it work

heres the wireshark capture of the send to exchange too, forgot to add it before
string StrFrom = "mailout@****.***";
                string StrTo = To.Text;
                string StrSubject = Subject.Text;
                MailMessage mail = new MailMessage();
                //mail.To.Add(To.Text);
                mail.Body = Editor1.XHTML;
                mail.IsBodyHtml = true;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                //mail.Body = "<table><tr><td><span style='color:red;'>TEST</span></td><td>Tester!</td></tr></table>";
                //mail.From = new MailAddress("mailout@*****.***");
                //mail.Subject = Subject.Text;

                OutPut2.Text = mail.Body;

                SmtpClient sMail = new SmtpClient("ch-ex.****.****");
                sMail.DeliveryMethod = SmtpDeliveryMethod.Network;
                sMail.Credentials = new NetworkCredential("mailout@****.****", "****");
                sMail.UseDefaultCredentials = false;
                sMail.Send(StrFrom, StrTo, StrSubject, mail);

Open in new window

Capture.PNG
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that code didnt work at all i didnt even type html just plain text and the other worked in plain text :S
Hmm, I'm not sure how that couldn't work.  Can you post your complete code?  Are you getting any exceptions, or the code just runs without complaining but no message is delivered?

Also, the entire communication between the sending computer and the mail server should occur on TCP port 25, so you can set a a display filter to "tcp.port == 25" or a capture filter to "tcp port 25" in Wireshark (the capture you have above seems to show regular Windows networking stuff, entirely unrelated to SMTP-based mail transfer) - and you can save the capture to a .cap file and post it, as opposed to a screen shot.
i just sent an email with both the working and none working code whilst doing a capture with "tcp.port == 25" applied no results returned, are you sure it uses smtp port 25 for sending email via web?
Yes, it would definitely be port 25 unless you have specified something to the contrary in the SmtpClient's constructor, or in your web.config; or if it's a secure connection in which case it'd be 465, I believe.

Here's a capture of an SMTP transaction in Wireshark (rename the the file so the extension is .pcap).
tcp.port.25.example.pcap.txt
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks