Solved

c# send mail func trouble

Posted on 2011-02-24
20
220 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 74

Expert Comment

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

Author Comment

by:awilderbeast
Comment Utility
still the same, absolutley nothing is happening :S

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

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
this broke it too
asdasda<span style="color: red; ">sdasdas</span>

Open in new window

0
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
0
 
LVL 1

Author Comment

by:awilderbeast
Comment Utility
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 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:awilderbeast
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now