Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

c# send mail func trouble

Posted on 2011-02-24
20
223 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:awilderbeast
  • 11
  • 3
  • 3
  • +2
20 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34969418
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34969423
i wish i new
nothing happens at all

no exceptions no mail arrives :S

does the code look ok?
0
 
LVL 3

Expert Comment

by:LFLFM
ID: 34969566
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Author Comment

by:awilderbeast
ID: 34969582
still the same, absolutley nothing is happening :S

the first code still works fine though :S
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 34969743
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34969833
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34969839
this broke it too
asdasda<span style="color: red; ">sdasdas</span>

Open in new window

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 34970112
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34970254
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
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 34974766
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34978138
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34979355
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34979780
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
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 250 total points
ID: 34980605
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34980673
that code didnt work at all i didnt even type html just plain text and the other worked in plain text :S
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34980930
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
 
LVL 1

Author Comment

by:awilderbeast
ID: 34995742
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35021785
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
 
LVL 1

Assisted Solution

by:awilderbeast
awilderbeast earned 0 total points
ID: 35025201
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
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35067668
Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question