I have a Contact us form where the user inputs certain data and it sends to a email address ive specified. This is working well however i cannot format the email properly for example i want the email address they enter to come up as the from address in the
email sent to my inbox. Im also looking to send the "name field" which i cannot do at the moment, in my code. It will only take 2 arguments and i cannot add any other fields into it. Would appreciate if anyone could help me with
this, Thanks
Thanks for the example, i just noticed something in my code, in my web config i have my system.net mail settings and i have smtp from ="test@gmail.com" im guessing this is making the test@gmail.com email address to show up as the from address when the email
is sent. How should i change that?
nt86
Member
19 Points
42 Posts
Formatting Email sent in c#
Feb 08, 2010 02:44 PM|LINK
I have a Contact us form where the user inputs certain data and it sends to a email address ive specified. This is working well however i cannot format the email properly for example i want the email address they enter to come up as the from address in the email sent to my inbox. Im also looking to send the "name field" which i cannot do at the moment, in my code. It will only take 2 arguments and i cannot add any other fields into it. Would appreciate if anyone could help me with this, Thanks
protected void btnContact_Click(object sender, EventArgs e) { SendMail(txtEmail.Text, txtComments.Text); } private void SendMail(string from, string body) { string mailServerName = "smtp.gmail.com"; MailMessage MyMailmessage = new MailMessage(from, "address@mail.com", "password", body); MyMailmessage.Subject = txtEmail.Text; SmtpClient mailClient = new SmtpClient(); mailClient.EnableSsl = true; mailClient.Host = mailServerName; mailClient.Send(MyMailmessage); }kushalrdalal
Contributor
7130 Points
1273 Posts
Re: Formatting Email sent in c#
Feb 08, 2010 03:51 PM|LINK
The from you can specify as txtFrom.Text in your
My Blog
LinkedIn Profile
ramiramilu
All-Star
95463 Points
14106 Posts
Re: Formatting Email sent in c#
Feb 08, 2010 05:48 PM|LINK
Hello,
I want t oshpw you a sample email body formation in html -
StringBuilder SbBody = new StringBuilder(); SbBody.Append("Customer Details - <br/>"); SbBody.Append("Name - " + txtName.Text + "<br/>"); SbBody.Append("Address - " + txtAddress.Text + "<br/>"); SbBody.Append("Email - " + txtEmail.Text + "<br/>"); SbBody.Append("Phone - " + txtPhone.Text + "<br/>"); SbBody.Append("Comments - " + txtComments.Text + "<br/>"); SbBody.Append("<hr/>");There txtName.Text is customer name textbox in the contact page...and i think the other fields are self explanatory as the preceeding....
And to send an html email....
MailMessage m = new MailMessage(); SmtpClient sc = new SmtpClient(); try { m.From = new MailAddress("from@gmail.com", "Display name"); m.To.Add(new MailAddress("to@domain.com", "Display name To")); m.CC.Add(new MailAddress("CC@yahoo.com", "Display name CC")); //similarly BCC m.Subject = "Test1"; m.IsBodyHtml = true; m.Body = " This is a Test Mail"; //To attache files FileStream fs = new FileStream("E:\\TestFolder\\test.pdf", FileMode.Open, FileAccess.Read); Attachment a = new Attachment(fs, "test.pdf", MediaTypeNames.Application.Octet); m.Attachments.Add(a); //To embed images in email string str = "<html><body><h1>Picture</h1><br/><img src=\"cid:image1\"></body></html>"; AlternateView av = AlternateView.CreateAlternateViewFromString(str, null,MediaTypeNames.Text.Html); LinkedResource lr = new LinkedResource("E:\\Photos\\hello.jpg", MediaTypeNames.Image.Jpeg); lr.ContentId = "image1"; av.LinkedResources.Add(lr); m.AlternateViews.Add(av); // smtp configuration settings... sc.Host = "smtp.gmail.com"; sc.Port = 587; sc.Credentials = new System.Net.NetworkCredential(“from@gmail.com","Password"); sc.EnableSsl = true; sc.Send(m); } catch (Exception ex) { Response.Write(ex.Message); }And in the above cse in the m.body...u can use the stringbuilder string to form html email body.....
And also thr Display name in the To, FROM address are self explanatory....
Hope this helps....
Thanks.
JumpStart
samuel24
Member
167 Points
64 Posts
Re: Formatting Email sent in c#
Feb 09, 2010 04:33 AM|LINK
Hi In Asp.net Email Sending we can format the sending text with Html Tags.
Try this link for sending Email.
http://dotetpgm.blogspot.com
http://www.dotnetpgm.blogspot.com
Vipindas
Contributor
5514 Points
810 Posts
Re: Formatting Email sent in c#
Feb 09, 2010 05:10 AM|LINK
Use HtmlTextWriter Class
http://www.vbdotnetheaven.com/UploadFile/scottlysle/HtmTextWriterIntro11132006230833PM/HtmTextWriterIntro.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx
nt86
Member
19 Points
42 Posts
Re: Formatting Email sent in c#
Feb 09, 2010 01:42 PM|LINK
Thanks for the example, i just noticed something in my code, in my web config i have my system.net mail settings and i have smtp from ="test@gmail.com" im guessing this is making the test@gmail.com email address to show up as the from address when the email is sent. How should i change that?
<system.net> <mailSettings> <smtp from="test@gmail.com"> <network host="smtp.gmail.com" password="password" port="587" userName="test@gmail.com" /> </smtp> </mailSettings> </system.net>MasterV23
Member
113 Points
318 Posts
Re: Formatting Email sent in c#
Feb 09, 2010 02:16 PM|LINK
You should be using "System.Net.Mail"
Then use MailMessage
Vipindas
Contributor
5514 Points
810 Posts
Re: Formatting Email sent in c#
Feb 10, 2010 05:58 AM|LINK
http://www.aspxcode.net/free-asp-net-data-sample-source-code.aspx?Topics=how%20to%20sent%20Mail%20by%20System.Net.Mail.MailMessage
http://www.codeproject.com/KB/aspnet/EmailApplication.aspx
sameer_khanj...
Contributor
7060 Points
1378 Posts
Re: Formatting Email sent in c#
Feb 10, 2010 06:08 AM|LINK
Formating an larg mail with fix Template(formate) i will suggest you please not use string operation to create partcular formate.
regardign this you can use XSLT file to generate HTML code and send this html code as string.
YOu can study XSLt from http://www.w3.org/TR/xslt
http://aspalliance.com/1296_Transform_XML_into_HTML_Using_XSLT
sameer.khanjit@gmail.com
View Blog
Click "Mark as Answer" on the post that helped you.
Mohammed Ask...
Contributor
2370 Points
535 Posts
Re: Formatting Email sent in c#
Feb 10, 2010 06:30 AM|LINK
Search
"sending html email asp.net"
http://askarnet.wordpress.com