|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Articles
Join Date: Apr 2001
Posts: 0
|
Article Discussion
This is an article discussion thread for discussing the SitePoint article, "Sending Web eMail in ASP.NET"
|
|
|
|
|
|
#2 |
|
SitePoint Member
Join Date: Jan 2004
Location: Sydney AUSTRALIA
Posts: 2
|
great article that covers the basics. Quick question - what are the advantages of using the codebehind page rather than using script blocks?
|
|
|
|
|
|
#3 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: May 2003
Location: San Diego
Posts: 795
|
Using script blocks, otherwise known as code declaration blocks requires interpretation similar to classic ASP. Code-behind allows for compilation which, in larger-scale apps could boost performance.
The RTFM people are coming, I can hear them, but to keep the explanation concise, there you go. |
|
|
|
|
|
#4 |
|
SitePoint Community Guest
Posts: n/a
|
A very good article for quick revision .
But I want to know the properties used to pass the sender name also because all mail services show from : "satya"<satya_chhikara@centrum.cz> here my name is also passed to reciver . How this can be possible without passing . I want to know the property of mail class that passes sender name also like message.to , message.from Please mail me at satya_chhikara@centrum.cz |
|
|
|
#5 |
|
SitePoint Community Guest
Posts: n/a
|
Very Handy coding...really useful if one decided to implement.
Thanks Bye. |
|
|
|
#6 |
|
Snowboarders die even younger
![]() ![]() ![]() ![]() ![]() Join Date: Feb 2005
Posts: 734
|
even though pretty simple its just one of those improvements ( there are many more) that makes your life easier with asp.net compared to long forgotten asp
![]() |
|
|
|
|
|
#7 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: Cape Town, South Africa
Posts: 1,587
|
Hey guys
I know how to send web emails. But now i want to send an email through a server that requires authentication. How do i send the username and password to the server in the code. Also i want the email to be from : Name Surname and wen u hit reply it replies to the right email address. Like wen somebody emails you from outlook. I hope someone can understand wat i am talking about. Thanks |
|
|
|
|
|
#8 | |
|
Snowboarders die even younger
![]() ![]() ![]() ![]() ![]() Join Date: Feb 2005
Posts: 734
|
Quote:
|
|
|
|
|
|
|
#9 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2003
Location: Springfield, MO
Posts: 1,866
|
For showing the results like you want something like this would work:
Code:
<%@ Page Language="VB" Debug="True" %>
<%@ Import Namespace="System.Web.Mail"%>
<html>
<head>
<title>Contact Form</title>
<script runat="server">
Sub SendMail(s as Object, e as EventArgs)
Dim objMail as New MailMessage()
objMail.To="emailaddress@domain.com"
objMail.From=txtEmail.Text
objMail.Subject="Contact Us"
objMail.BodyFormat=MailFormat.Text
objMail.Body="Name: " & txtName.Text & vbcrlf
objMail.Body &="Email Address: " & txtEmail.Text & vbcrlf
objMail.Body &="Comments: " & txtComments.Text & vbcrlf
SmtpMail.SmtpServer="localhost"
SmtpMail.Send(objMail)
txtName.Text=""
txtEmail.Text=""
txtComments.Text=""
lblMessage.Text="The message has been sent!"
End Sub
Sub Reset(s as Object, e as EventArgs)
'clear the contents of the form
txtName.Text=""
txtEmail.Text=""
txtComments.Text=""
End Sub
</script>
</head>
<body>
<form runat="server">
<table width="400" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>Full Name:</td>
<td><asp:TextBox id="txtName" runat="server"/>
</tr>
<tr>
<td>E-mail:</td>
<td><asp:TextBox id="txtEmail" runat="server"/>
</tr>
<tr>
<td valign="top">Comments:</td>
<td><asp:TextBox id="txtComments" TextMode="MultiLine" Rows="4" Columns="15" runat="server"/>
</tr>
<tr>
<td colspan="2" align="center"><br><asp:Button id="btnSubmit" runat="server" OnClick="SendMail" Text="Submit"/>
<asp:Button id="btnReset" runat="server" OnClick="Reset" Text="Reset"/><br>
<asp:Label id="lblMessage" runat="server"/>
</tr>
</table>
</form>
</body>
</html>
Last edited by rbutler; May 25, 2007 at 15:06. |
|
|
|
|
|
#10 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: Cape Town, South Africa
Posts: 1,587
|
Hi
Thanks for the replies. But i want it in outlook 2 say: eg. From: My Name and wen i click reply it goes to myemail@isp.com I dnt know if this is possible. There must be some way to authenticate on a secure mail server. if not, it would be very stupid of MSOh well. Thanks in advance |
|
|
|
|
|
#11 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2003
Location: Springfield, MO
Posts: 1,866
|
Then replace the to and from in the object to your settings.
Code:
objMail.To="youremail@email.com" objmail.From="Your Name Here" |
|
|
|
|
|
#12 |
|
SitePoint Community Guest
Posts: n/a
|
dude it is giving an error
System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x8004020F): The server rejected one or more recipient addresses. The server response was: 550 minutes or do not have SMTP Authentication turned on in your email client. --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) --- End of inner exception stack trace --- at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) at System.Web.Mail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at sanmail.WebForm1.Button1_Click(Object sender, EventArgs e) in d:\inetpub\wwwroot\sanmail\WebForm1.aspx.vb:line 36 |
|
|
|
#13 |
|
SitePoint Community Guest
Posts: n/a
|
hello sir
It's all are fine but I don't know the server-name. I meantIi don't know how I write in "smtp.server-name.com" section |
|
|
|
#14 | |
|
Born to Code
![]() ![]() ![]() ![]() ![]() Join Date: Feb 2005
Location: Try search 'rainadaman' on Google.
Posts: 792
|
Quote:
|
|
|
|
|
|
|
#15 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Sep 2004
Location: India
Posts: 235
|
Quote:
SmtpMail.SmtpServer = "localhost"; Good article |
|
|
|
|
|
|
#16 |
|
Born to Code
![]() ![]() ![]() ![]() ![]() Join Date: Feb 2005
Location: Try search 'rainadaman' on Google.
Posts: 792
|
![]() |
|
|
|
|
|
#17 |
|
SitePoint Member
Join Date: Mar 2004
Location: xFriday - Faridabad
Posts: 22
|
This is a good article and it worked both from local server and remote server for me but what if I want to show my name as "Some Name" and send it from a non standard port on remote or local SMTP Server :p?
|
|
|
|
|
|
#18 |
|
SitePoint Member
Join Date: Mar 2004
Location: xFriday - Faridabad
Posts: 22
|
ok I figured out the name thing and I just need to mention it as
"Some Name<someemail@some-domain.com>" and thats all there is to it. :-) Cheers!! |
|
|
|
|
|
#19 | |
|
SitePoint Member
Join Date: Mar 2004
Location: xFriday - Faridabad
Posts: 22
|
Quote:
|
|
|
|
|
|
|
#20 |
|
SitePoint Member
Join Date: Jun 2005
Posts: 16
|
I know this should work and it has clearly worked for others. However when I run this on my browser and enter my email address I get the following error (with line 21 highlighted)
Exception Details: System.Runtime.InteropServices.COMException: The transport failed to connect to the server. Source Error: Line 19: objMail.BodyFormat = MailFormat.Text Line 20: SmtpMail.SmtpServer = " smtp.your-server.com" Line 21: SmtpMail.Send(objMail) Line 22: Else Line 23: lblResponse.Text = "Please enter an email address." (Please also note that I have configured IIs to send email and I am using the following Microsoft.net beta version -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032 ). Any help would be greatly appreciated |
|
|
|
|
|
#21 |
|
SitePoint Community Guest
Posts: n/a
|
The author forgot that most SMTP servers
need a password. So his code *VERY* rare ever works. Sheezesss. Great way to "teach" asp.net |
|
|
|
#22 |
|
SitePoint Community Guest
Posts: n/a
|
this requires 1.1 of .net framework, but allows authentication using smtpmail
objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "usrename") 'set your username here objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password") 'set your password here |
|
|
|
#23 |
|
SitePoint Enthusiast
![]() Join Date: Aug 2005
Posts: 35
|
This is the entire function that I use. Just change the obvious strings. You can find one of the places I implemented this at
My directories list at http://www.adacprogramming.com/Utili...orieslist.aspx if you want to see how it works. string strMailBody; strMailBody = "Directory in question: " + this.txtDirectory.Text + "<BR>Comment br> " + this.txtComment.Text;System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); mail.From = this.txtFromAddress.Text; mail.To = "Anyone@someserver.com"; mail.Subject = "*******Directory List Comment********"; mail.Body = strMailBody; mail.BodyFormat = System.Web.Mail.MailFormat.Html; string Username = "MyUserName"; string Password = "MyPassword"; try { mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //none, cdoBasic, NTLM mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Username ); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Password ); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", false); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 60); string SmptServer = "mail.servername.com"; System.Web.Mail.SmtpMail.SmtpServer = SmptServer ; System.Web.Mail.SmtpMail.Send(mail); this.lblMessageSent.Visible = true; } catch{} |
|
|
|
|
|
#24 |
|
O Rly??
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2000
Location: Milwaukee
Posts: 733
|
ADAC,
There's no point in using try/catch if you don't do anything when an exception occurs. Plus it wastes overhead. Also I'd recommend putting the From Address, username, password, and smtp server name in the web.config. It's always better to modify a config file than having to modify code and recompile if things ever change. |
|
|
|
|
|
#25 | |
|
SitePoint Enthusiast
![]() Join Date: Aug 2005
Posts: 35
|
Quote:
I would normally agree with the try catch except in my code the mail portion is a secondary consideration. I also send this to a database. This just keeps a possible server glitch from showing to the user. In my complete code the error would be recorded so I would know about it. Also agree on the web config. I use this code in several places in my website. Would be a pain to change them with out web.config. Thanks Dwayne |
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 11:55.






if not, it would be very stupid of MS
br> " + this.txtComment.Text;


Linear Mode
