Go Back   SitePoint Forums > Forum Index > Program Your Site > .NET
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Aug 19, 2004, 20:03   #1
ArticleBot
SitePoint Articles
 
ArticleBot's Avatar
 
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"
ArticleBot is offline   Reply With Quote
Old Nov 8, 2004, 16:18   #2
jmaonline
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?
jmaonline is offline   Reply With Quote
Old Nov 8, 2004, 16:37   #3
zakruvalcaba
SitePoint Guru
 
zakruvalcaba's Avatar
 
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.
zakruvalcaba is offline   Reply With Quote
Old Nov 22, 2004, 05:03   #4
satya chhikara
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
  Reply With Quote
Old Apr 9, 2005, 03:33   #5
Vivek Jiwtode
SitePoint Community Guest
 
Posts: n/a
Very Handy coding...really useful if one decided to implement.
Thanks
Bye.
  Reply With Quote
Old Apr 10, 2005, 07:22   #6
igor.kudela
Snowboarders die even younger
 
igor.kudela's Avatar
 
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
igor.kudela is offline   Reply With Quote
Old Apr 11, 2005, 06:28   #7
NightStalker-DNS
SitePoint Wizard
 
NightStalker-DNS's Avatar
 
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
NightStalker-DNS is offline   Reply With Quote
Old Apr 11, 2005, 11:40   #8
igor.kudela
Snowboarders die even younger
 
igor.kudela's Avatar
 
Join Date: Feb 2005
Posts: 734
Quote:
Originally Posted by NightStalker-DNS
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
i am not 100% sure but its not possible as far as i know u need to find an open relay , but i might be wrong
igor.kudela is offline   Reply With Quote
Old Apr 11, 2005, 13:41   #9
rbutler
SitePoint Wizard
 
rbutler's Avatar
 
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>
Where the from field is a text box control that when submitted to an email client such as Outlook, hitting the reply would automatically fill in their email address. As far as authentication, not sure, I've only used relay.

Last edited by rbutler; May 25, 2007 at 15:06.
rbutler is offline   Reply With Quote
Old Apr 12, 2005, 01:47   #10
NightStalker-DNS
SitePoint Wizard
 
NightStalker-DNS's Avatar
 
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 MS

Oh well. Thanks in advance
NightStalker-DNS is offline   Reply With Quote
Old Apr 12, 2005, 07:16   #11
rbutler
SitePoint Wizard
 
rbutler's Avatar
 
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"
rbutler is offline   Reply With Quote
Old Apr 29, 2005, 07:26   #12
sangeeth
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
  Reply With Quote
Old May 24, 2005, 21:02   #13
vivek
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
  Reply With Quote
Old May 25, 2005, 01:32   #14
rainadaman
Born to Code
 
rainadaman's Avatar
 
Join Date: Feb 2005
Location: Try search 'rainadaman' on Google.
Posts: 792
Quote:
Originally Posted by vivek
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
try localhost there.
rainadaman is offline   Reply With Quote
Old May 25, 2005, 03:17   #15
ankitaneja1
SitePoint Addict
 
ankitaneja1's Avatar
 
Join Date: Sep 2004
Location: India
Posts: 235
Quote:
Originally Posted by rainadaman
try localhost there.
yes vivek rain is right you can write
SmtpMail.SmtpServer = "localhost";

Good article
ankitaneja1 is offline   Reply With Quote
Old May 25, 2005, 23:26   #16
rainadaman
Born to Code
 
rainadaman's Avatar
 
Join Date: Feb 2005
Location: Try search 'rainadaman' on Google.
Posts: 792
Thumbs up

rainadaman is offline   Reply With Quote
Old May 30, 2005, 04:57   #17
xfriday
SitePoint Member
 
xfriday's Avatar
 
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?
xfriday is offline   Reply With Quote
Old May 30, 2005, 04:58   #18
xfriday
SitePoint Member
 
xfriday's Avatar
 
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!!
xfriday is offline   Reply With Quote
Old May 30, 2005, 04:59   #19
xfriday
SitePoint Member
 
xfriday's Avatar
 
Join Date: Mar 2004
Location: xFriday - Faridabad
Posts: 22
Red face

Quote:
Originally Posted by NightStalker-DNS
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 MS

Oh well. Thanks in advance
You can check my last post
xfriday is offline   Reply With Quote
Old Jun 16, 2005, 07:14   #20
Caitriona
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
Caitriona is offline   Reply With Quote
Old Aug 21, 2005, 22:12   #21
Susan
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
  Reply With Quote
Old Sep 8, 2005, 04:54   #22
james harris
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
  Reply With Quote
Old Sep 10, 2005, 17:47   #23
ADACProgramming
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>Commentbr> " + 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{}
ADACProgramming is offline   Reply With Quote
Old Sep 10, 2005, 21:34   #24
JakeJeck
O Rly??
 
JakeJeck's Avatar
 
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.
JakeJeck is offline   Reply With Quote
Old Sep 14, 2005, 18:57   #25
ADACProgramming
SitePoint Enthusiast
 
Join Date: Aug 2005
Posts: 35
Quote:
Originally Posted by JakeJeck
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.


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
ADACProgramming is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 11:55.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved