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

LVL 1
awilderbeastAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Daniel Van Der WerkenConnect With a Mentor Independent ConsultantCommented:
I just sent myself mail using your non-working code.  Can you give us more information?  What's in your body, for example, etc.
0
 
käµfm³d 👽Commented:
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.
0
 
awilderbeastAuthor Commented:
i wish i new
nothing happens at all

no exceptions no mail arrives :S

does the code look ok?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LFLFMCommented:
try this:
sMail.UseDefaultCredentials = false;
                sMail.DeliveryMethod = SmtpDeliveryMethod.Network;
                sMail.Credentials = new NetworkCredential("mailout@****", "*****");
                sMail.UseDefaultCredentials = false;
                sMail.Send(mail);

Open in new window

0
 
awilderbeastAuthor Commented:
still the same, absolutley nothing is happening :S

the first code still works fine though :S
0
 
awilderbeastAuthor Commented:
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
0
 
awilderbeastAuthor Commented:
this broke it too
asdasda<span style="color: red; ">sdasdas</span>

Open in new window

0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
0
 
awilderbeastAuthor Commented:
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
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
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.
0
 
awilderbeastAuthor Commented:
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?
0
 
käµfm³d 👽Commented:
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  : \
0
 
awilderbeastAuthor Commented:
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
0
 
Todd GerbertConnect With a Mentor IT ConsultantCommented:
Line 19 in your snippet is an overload of SmtpClient.Send that expects something like sMail.Send("sender@domain.com", "recipient@domain.com", "This is a test message", "This is the complete body of the message") (the last parameter is expected to be a string, which is the message, but you're trying to pass it a MailMessage object).

I am unable to duplicate your problem so I can't be sure - but I wonder if you don't set the BodyEncoding (i.e. leave it at it's default of Encoding.ASCII) is there any change?
using (SmtpClient smtp = new SmtpClient("ch-ex.****.****"))
{
	smtp.Credentials = new NetworkCredential("mailout@****.****", "****L");

	using (MailMessage message = new MailMessage(To.Text, "mailout@****.****"))
	{
		message.Subject = Subject.Text;
		message.Body = Editor1.XHTML;
		message.IsBodyHtml = true;

		smtp.Send(message);
	}
}

Open in new window

0
 
awilderbeastAuthor Commented:
that code didnt work at all i didnt even type html just plain text and the other worked in plain text :S
0
 
Todd GerbertIT ConsultantCommented:
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.
0
 
awilderbeastAuthor Commented:
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?
0
 
Todd GerbertIT ConsultantCommented:
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
0
 
awilderbeastConnect With a Mentor Author Commented:
i found out the error now!

i got a new bit of code for emailing which works

the problem was the editor that i was emailing had <template></template> tags in it which arent valid in html email so it wasnt going

all my tests have now gone through fine without those tags in, in which those tags will actually be stripped and replaced with data before being emailed anyway

thanks for your help though! :)
try
        {
            
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("mailout@***.***");
            mail.To.Add("alex@**.**");
            mail.Subject = "Test Mail";
            mail.IsBodyHtml = true;

            string htmlBody;
            string plainBody;
            htmlBody = Editor1.XHTML;
            plainBody = Editor1.PlainText;
                      
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
            AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, null, "text/plain");

            LinkedResource bookpic = new LinkedResource("/book_32.png");
            bookpic.ContentId = "book";
            htmlView.LinkedResources.Add(bookpic);
            
            //mail.Body = htmlBody;

            mail.AlternateViews.Add(htmlView);
            mail.AlternateViews.Add(plainView);

            SmtpClient SmtpServer = new SmtpClient("ch**.****.*****al");
            SmtpServer.Port = 25;
            SmtpServer.UseDefaultCredentials = true;
            SmtpServer.Send(mail);

            OutPut2.Text = plainBody;

        }
        catch (Exception ex)
        {
            OutPut2.Text += "<br />" + Convert.ToString(ex);
        }

Open in new window

0
 
awilderbeastAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.