Web Hosting Forum | Lunarpages

Author Topic: Sending email with ASP.net  (Read 16307 times)

katrina1

  • Guest
Sending email with ASP.net
« on: February 21, 2010, 12:24:07 AM »
This item was provided to us by a user. I have not tested it since I don't do ASP.net but thought I would pass it along in case it is helpful to anyone.

"Here is a simple block of code that I got to work for ASP.NET 2.0 in C# to send an email."

//+++ Start Code block ++++

System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = yourhosthere;
mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"] = 10;

mail.To = emailTo;
mail.Cc = emailCC;
mail.Bcc = emailBCC;
mail.From = emailFrom;
mail.Subject = subject;
mail.Body = message;
mail.Priority = priority;

System.Web.Mail.SmtpMail.SmtpServer = yourhosthere;
System.Web.Mail.SmtpMail.Send(mail);

//+++ End Code block ++++

Offline Mitch

  • Berserker Poster
  • *****
  • Posts: 12641
    • MitchKeeler.com
Re: Sending email with ASP.net
« Reply #1 on: February 22, 2010, 04:58:08 AM »
Thanks for sharing Kat! :)
New to Web Site Hosting? Check Out the Lunarpages Blog Hosting Guide!


Follow us @lunarpages on Twitter!
Important Threads: Read This Before Posting! | Lunarforums Rules! | Mitch's Link of the Day!
Also, be sure to check out and subscribe to the Lunartics Blog and the Lunarpages Newsletter !

Need Web Hosting Help? Check out the Lunarpages Web Hosting Wiki. It has tons of tips, tutorials and resources!

MWaqas

  • Guest
Re: Sending email with ASP.net
« Reply #2 on: February 10, 2011, 05:20:35 AM »
Hey Kat.

This would work on the servers running with local mail server installed (old windows servers), the latest windows servers use remote Mail-Server called Smarter Mail which must need SMTP authentication to send emails out via code; one will have to use SMTP-Authentication which means to use an actual email address and its password in the code to send emails out, hence any emails being sent out from the webapplications in PHP/Perl/ASP/ASP.net need the SMTP verification to be done prior to the mail() command in PHP with any email account, created from Plesk's "Remote Email" section.

The smtp verification can be done either with smtp server address "mail.your-domain.com" if you are using asp.net please use CDOSYS (with SMTP authentication) and if you are using PHP please use php-pear module. Samples of both codes are provided below:

---Pear module with Smtp-auth----
require_once "Mail.php";
$from = "<you@your.com>";
$to = "Waqas <m.waqas@lunarpages.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.your.com";
$username = "you@your.com";
$password = "pass_of_you@your.com";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}

Here is a working example of -----------====CDOSYS====---------- without the opening ASP tags:

 Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
 Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
 Const cdoAnonymous = 0 'Do not authenticate
 Const cdoBasic = 1 'basic (clear-text) authentication
 Const cdoNTLM = 2 'NTLM
 Set objMessage = Server.CreateObject("CDO.Message")
 objMessage.Subject = "Example CDO Message"
 objMessage.Sender = "me@mydomain.com"
 objMessage.From = "me@mydomain.com"
 objMessage.To = "recipient@test.com"
 objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."
 '==This section provides the configuration information for the remote SMTP server.
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 'Name or IP of Remote SMTP Server
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"
 'Type of authentication, NONE, Basic (Base64 encoded), NTLM
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
 'Your UserID on the SMTP server
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "you@your.com"
 'Your password on the SMTP server
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password_of_you@your.com"
 'Server port (typically 25)
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
 'Use SSL for the connection (False or True)
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
 'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
 objMessage.Configuration.Fields.Update
 objMessage.Send

katrina1

  • Guest
Re: Sending email with ASP.net
« Reply #3 on: February 10, 2011, 01:41:24 PM »
Woohoo! Thanks for the update!

Offline fstjohn

  • Intergalactic Superstar
  • *****
  • Posts: 180
    • http://www.mortgagecorp.com
Re: Sending email with ASP.net
« Reply #4 on: June 14, 2011, 06:15:37 AM »
let me start by saying I'm kinda not all that well versed with Windows servers and ASP

Tried the mail script - and while it DOES send mail, it doesnt send the info from the fields that the user fills out, am I missing something?

here's the example:
http://www.mimbanker.com/sendform.asp

and here's my code

Code: [Select]
<%
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objMessage = Server.CreateObject("CDO.Message")
objMessage.Subject = "Sent From Mimbanker"
objMessage.Sender = "formsubmit@mimbanker.com"
objMessage.From = "formsubmit@mimbanker.com"
objMessage.To = "formsubmit@mimbanker.com"
objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mimbanker.com"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "formsubmit@mimbanker.com"

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "temppass1"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMessage.Configuration.Fields.Update
objMessage.Send

%>




 

Share |