Email, ASP.NET amd authentication

thegriff

New Member
Messages
14
Reaction score
0
Points
0
I have set up a system where registration and login are required, and as part of teh registration process I confirm Email addresses by sending a message containing an authorization code - all pretty standard stuff.

The problem is that the only way I have found to send the email needs my cpanel login id and password, which I don't want to include in the ASP.NET code, or even in the web.config file if I can help it. Is there some authentication I have missed which doesn't use this?

Code:
[SIZE=2]System.Net.Mail.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailMessage[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] mail = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Net.Mail.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailMessage[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]();[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] body = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Hello "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + mu.UserName + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" and thank you for registering your new account...<br/>";[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]mail.Body = body;[/SIZE]
[SIZE=2]mail.IsBodyHtml = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]mail.To.Add([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailAddress[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](mu.Email));[/SIZE]
[SIZE=2]mail.From = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Net.Mail.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailAddress[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"NoReply@XXX.x10hosting.com"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"NoReply@XXX.x10hosting.com"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].UTF8);[/SIZE]
[SIZE=2]mail.Subject = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"New account registration"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]mail.SubjectEncoding = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].UTF8;[/SIZE]
[SIZE=2]mail.Priority = System.Net.Mail.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailPriority[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].High;[/SIZE]
[SIZE=2]System.Net.Mail.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SmtpClient[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] smtp = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Net.Mail.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SmtpClient[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]();[/SIZE]
[SIZE=2]smtp.Host = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mail.XXX.x10hosting.com"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]smtp.Port = 26;[/SIZE]
[SIZE=2]smtp.Credentials = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Net.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]NetworkCredential[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"<my log in id>"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"<my log in password>"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]smtp.Send(mail);[/SIZE]
 

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
If you were using php you could use the sendmail() function, and I think I have found an ASP equivalent here, which uses something called CDOSYS.
 
Last edited:

srikri

New Member
Messages
10
Reaction score
0
Points
0
Hi thegriff,

Your code looks fine... I don't think there is any way you can bypass smtp authentication in asp.net. If you are not comfortable in including password in code or web.config, use php (only to send mail). Below is the php code for sending mail.

<?php
$sendTo = "xxx@xxx.x10hosting.com";
$subject = "Your subject goes here";
$headers = "From: <your mail id>";
$message = "testing";
mail($sendTo, $subject, $message, $headers);
?>
 

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
Your code looks fine... I don't think there is any way you can bypass smtp authentication in asp.net.
You can though:
Code:
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.Send
set myMail=nothing
%>
(From the w3schools link in my earlier post.)
I'm just not sure whether it will work here since X10 uses Mono. I would recommend you use php generally though since that is a much more common language.
 
Last edited:

thegriff

New Member
Messages
14
Reaction score
0
Points
0
Re: Email, ASP.NET amd authentication [SOLVED]

Thank you for the replies - I have found a solution!

The Authentication requirement is a red herring; simply remove all reference to it and the problem goes away. The following code sends a email to a user (of the standard ASP.NET membership system):
Code:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]<summary>
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] Send an email to the user.
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]</summary>
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]<param name="mu">[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]User to send email to[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]</param>
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]<param name="body">[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]Text to send[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]</param>
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]<param name="subject">[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]Seubject line[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]</param>
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]<param name="from">[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]Subaccount to send from. If null or empty, NoReply will be used.[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]</param>
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]static[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SendUserEmail([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MembershipUser[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] mu, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] body, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] subject, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] from)
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].IsNullOrEmpty(from))
{
from = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"NoReply"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]try
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailMessage[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] mail = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailMessage[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]();
mail.Body = body;
mail.IsBodyHtml = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
mail.To.Add([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailAddress[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](mu.Email));
mail.From = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailAddress[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](from + strEmailAccountBase, from + strEmailAccountBase, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].UTF8);
mail.Subject = subject;
mail.SubjectEncoding = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].UTF8;
mail.Priority = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MailPriority[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Normal;
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SmtpClient[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] smtp = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SmtpClient[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]();
smtp.Host = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mail.XXX.x10hosting.com"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
smtp.Port = 26;
smtp.Send(mail);
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]catch[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Exception[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ex)
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]throw[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]ApplicationException[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Could not send email"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], ex);
}
}
[/SIZE]
 
Top