When someone registers on my website he/she also needs to fill in his/her e-mail address.
After clicking on the ''Register''-button the system needs to send an automated e-mail to the e-mail address which is written in the textbox.
Anyone who can help me with this?
Thanks in advance!
To send email in asp.net you will want to look into System.Net.Mail. There are two steps two send mail from within asp.net
1) Email Account Settings - This can be set up globally in your web.config file
<system.net>
<mailSettings>
<smtp from="test#foo.com">
<network host="host" port="25" userName="username" password="password" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
2) Setting up your message - In your register pages code behind
MailMessage message = new MailMessage();
message.From = new MailAddress("sender#sender.com");
message.To.Add(new MailAddress("email#email.com"));
message.Subject = "subject";
message.Body = "content";
SmtpClient client = new SmtpClient();
client.Send(message);
http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx
Here is a simple instruction on how to send e-mail from .NET code: http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx
The main keyword you are looking for is System.Net.Mail.
The following script is a good starting place.
using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage()) {
System.Text.StringBuilder body = new System.Text.StringBuilder("Your message");
System.Net.Mail.SmtpClient() smtp = new System.Net.Mail.SmtpClient();
mail.To.Add("yourUser#email.com");
mail.From = new System.Net.Mail.MailAddress("yourFrom#email.com");
mail.Subject = "a Subject";
mail.Body = body.ToString();
try {
smtp.Send(mail);
} catch (Exception ex) {
// handle your exception here..
}
}
This assumes that you have some details in your config about the SmtpClient. Like this: http://msdn.microsoft.com/en-us/library/ms164240.aspx
If not, then you can provide the details to the client in the SmtpClient() constuctor. More info on that here: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
Here is a simple function to implement. You have to get network credentials if not anonymous and SMTP info about your SMTP relay server. Otherwise it should be straight forward.
using System.Net.Mail; //goes on top
//goes in your class
public void sendEmail(string emailMessage,
string emailSubject,
string emailAddress, string from,
string fromAddress, string emailCC,
string emailBCC)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromAddress, from);
msg.To.Add(emailAddress);
if (emailCC != null && emailCC.ToString().Length > 1)
msg.CC.Add(emailCC);
if (emailBCC != null && emailBCC.ToString().Length > 1)
msg.Bcc.Add(emailBCC);
msg.Priority = MailPriority.High;
msg.Subject = emailSubject;
msg.Body = emailMessage;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = info.SMTPServer;
client.Port = 25;
client.EnableSsl = true;
// client.UseDefaultCredentials = some System.Net.NetworkCredential var;
client.Credentials = info.networkCredentials;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Use SendAsync to send the message asynchronously
client.Send(msg);
}
catch
{
//handle exception
}
}
USAGE & Implementation:
this.sendEmail("test message", "your subject", "to#to.com","from person", "from#from.com","cc#cc.com","bcc#bcc.com");
Related
Good day, I'm a beginner from using ASP.net and SMTP Mailer
Heres my Question, I always encounter this Error when i send email from my local
and searched and tried the solutions around the net but not so lucky,
I hope someone point out what codes do i need and where i encounter this errror
Message = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
Heres my Code:
protected void btnSendEmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Value, txtName.Value);
smtpClient.Credentials = new System.Net.NetworkCredential("myUser#gmail", "password");
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true;
//Default port will be 25
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("myEmail#gmail.com");
message.Subject = txtSubject.Value;
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1#yoursite.com")
message.CC.Add("myEmail#gmail.com");
// You can specify Address directly as string
message.Bcc.Add(new MailAddress("myEmail#gmail.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtaMessage.Value;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.HeadersEncoding = System.Text.Encoding.UTF8;
// Send SMTP mail
smtpClient.Send(message);
lblSuccess.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblSuccess.Text = "Send Email Failed.";
}
}
i tried to make a simple codes for sending email try this
MailMessage mm = new MailMessage();
mm.From = new MailAddress("fromEmail");
mm.To.Add("toEmail");
mm.CC.Add("ccEmail");
mm.Subject = "StringSubject";
mm.Body = "BodySubject";
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "UsernameString";
NetworkCred.Password = "PasswordString";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
try
{
smtp.Send(mm);
}
catch (Exception)
{
}
Try this reference ASP Simple SMTP for C# and VB it helps me a lot for may smtp problem
Please have a look on google support team, what they are saying regarding sending mail from application.
https://support.google.com/mail/answer/78775?hl=en
Also following link can help you.
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
Goto Account of Gmail , then select
Connected apps & sites
Allow less secure apps: ON(if this is off you cannot send mails through apps,or your websites )
I have been trying for 2 days to get my ASP.NET webforms application to send an e-mail.
I have tried this using both outlook and gmail. I got the smtp information for both from this tutorial:
When I try using port 587 in the example below I get an error saying:
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
When I try using port 465 in the example below:
My application just hangs forever and my page never gets back my email function which is in the PageLoad.
A couple of things to note just incase one of these is messing me up:
I am using a standard VS2013 dev environment (running my web app in debug mode using F5)
I am using the same e-mail address for from, to, and gmail login credentials)
My ISP does not block these ports. (comcast cable)
I even went to the google DisplayUnlockCaptcha page to Allow access to my account
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
using (MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress))
{
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
}
This code should work fine for you
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
You'll need to set the delivery mode otherwise gmail will return an error
EDIT:
Throwing an 'using' around 'MailMessage' might also be a smart thing to do
It turns out that it is because of a GMail security setting.
https://www.google.com/settings/security/lesssecureapps
You have to enable access for less secure apps.
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("xxx#gmail.com");
mail.From = new MailAddress("yyy#gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yyy#gmail.com", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}``
http://www.c-sharpcorner.com/UploadFile/47548d/how-to-send-bulk-email-using-Asp-Net/
I am trying to send email through the web application using my client organisation's email server. The following is the code I am using.
MailMessage MyMailMessage = new MailMessage();
MyMailMessage.Subject = "Email testing";
MyMailMessage.From = new MailAddress("name#mydomain.com", "My name");
MyMailMessage.To.Add(new MailAddress(strEmail, "Applicant"));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.EnableSsl = true;
mySmtpClient.Send(Accepted);
Web.config file:
<mailSettings>
<smtp deliveryMethod="Network" from=" name#mydomain.com">
<network host="smtps.mydomain.com" port="465" enableSsl="true" defaultCredentials="true"
userName="myName" password="myPassword" />
</smtp>
</mailSettings>
It works perfectly fine when I use gmail smtp details or my local organisation's smtp details. For some reason, its not working and neither is it throwing any errors.
I have tried debugging and checked the exception which says 'timed out'.
I am not sure what else to check. Could someone please suggest a solution.
Note: I have also checked that no firewall is blocking port:465.
Thank you.
Kind regards,
Sud
You could try to test the connection by typing "telnet smtps.mydomain.com 465" in Command Promt(cmd).
Regards
Try this one:
var sysLogin="yourlogin#gmail.com";
var sysPass="y0urP#ss";
var sysAddress = new MailAddress(sysLogin, "Message from me!");
var receiverAddress = new MailAddress("mike#hotmail.com");
var smtp = new SmtpClient
{
Host = "smtp.gmail.com", //gmail example
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(sysLogin, sysPass)
};
using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" })
{
smtp.Send(message);
}
<system.net>
<mailSettings>
<smtp from="emialid.com">
<network host="domain.com" port="25" userName="emialid.com" password="******" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
public string SendEmailTest(String EmailMessage, String FromMail, String MailPassword, String MailServer, String To, String CC, String BCC, String DisplayName, String Subject, String Attachment)
{
try
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress;
fromAddress = new MailAddress(FromMail);
smtpClient.Host = MailServer;
smtpClient.Port = 25;
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(FromMail, MailPassword);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = SMTPUserInfo;
message.From = fromAddress;
message.To.Add(new MailAddress(To, DisplayName));
if (CC != "")
message.CC.Add(new MailAddress(CC, DisplayName));
if (BCC != "")
message.Bcc.Add(new MailAddress(BCC, DisplayName));
message.Subject = Subject;
message.IsBodyHtml = true;
message.Body = "Your Password is : " + EmailMessage;
if (Attachment != "")
message.Attachments.Add(new Attachment(Attachment));
message.Priority = MailPriority.High;
smtpClient.Send(message);
return "SendEmail";
}
catch (Exception ex)
{
return "Email :" + ex;
}
}
}
}
Have you verified that the client SMTP server has a valid SSL certificate installed and can accept connections on port 465?
If you are able to do so, run a packet sniffer such as WireShark to check the packet flow.
The SysIntenerals TcpView tool is a lighter-weight utility that would let you see the packet state as well.
Thank you all for your suggestions. I have fixed it, I just had to change the port number from 465 to 25.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sending email in .NET through Gmail
This mail code is working in localhost i.e on my computer but when i uopload it on server it is not working.
The error is : Failure sending mail. please tell me where is the problem.
if (Session["userinfo"] != null)
{
lblTest.Text = Session["userinfo"].ToString();
MailMessage msg = new MailMessage();
msg.From = new MailAddress("shop.bcharya#gmail.com");
msg.To.Add(new MailAddress("bcc#dr.com"));
msg.To.Add(new MailAddress("info#yzentech.com"));
msg.Subject = "Mail from BcharyaCorporation.online shopping site";
msg.Body = ""+lblTest.Text+" wants to buy some products. please contact with him/her";
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.gmail.com";
// sc.Port = 25;
sc.Credentials = new NetworkCredential("shop.bcharya#gmail.com", "mypassword");
sc.EnableSsl = true;
try
{
sc.Send(msg);
lblPayment.Text = "Sorry. Currently we are out of online payment service. We will contact you for payment process. Thank you for buying this product.";
}
catch (Exception ex)
{
lblPayment.Text=ex.Message.ToString();
Response.Write(ex.Message);
}
}
For gmail mail settings add Port number too
sc.Port = 587;
after this line
sc.Host = "smtp.gmail.com";
Only use port 587 and SSL if the SMTP server supports that (GMail and Hotmail for example). Some servers just use port 25 and no SSL.
Use below method and then check :
SmtpClient sc = new SmtpClient(string); //sends e-mail by using the specified SMTP server
You can use this below given code for sending email. Here sending the error details through email is one method. Try this code for sending email.
using System.Web.Mail
public static bool SendErrorEmail(string to, string cc, string bcc, string subject, string body, MailPriority priority, bool isHtml)
{
try
{
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress(“yourmail#domain.com”, “Your name”);
// You can specify the host name or ipaddress of your server
smtpClient.Host = “mail.yourdomain.com”; //you can specify mail server IP address here
//Default port is 25
smtpClient.Port = 25;
NetworkCredential info = new NetworkCredential(“yourmail#domain.com”, “your password”);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = info;
//From address will be given as a MailAddress Object
message.From = from;
message.Priority = priority;
// To address collection of MailAddress
message.To.Add(to);
message.Subject = subject;
// CC and BCC optional
if (cc.Length > 0)
{
message.CC.Add(cc);
}
if (bcc.Length > 0)
{
message.Bcc.Add(bcc);
}
//Body can be Html or text format;Specify true if it is html message
message.IsBodyHtml = isHtml;
// Message body content
message.Body = body;
// Send SMTP mail
smtpClient.Send(message);
}
}
return true;
}
catch (Exception ee)
{
Logger.LogError(ee, “Error while sending email to ” + toAddress);
throw;
}
}
While sending email, I get the following error:
The device is not ready at System.Net.Mail.SmtpClient.Send(MailMessage message).
The code is :
MailMessage mailMessage = new MailMessage(senderEmail, cleanRecipients)
{
Subject = string.empty,
Body = string.empty,
IsBodyHtml = false
};
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
In order to send an email you need an SMTP server, so make sure you have specified an SMTP server in the config file.
<system.net>
<mailSettings>
<smtp from="someaddress#mydomain.com">
<network host="mail.mydomain.com" password="secret" port="25" userName="someaddress#mydomain.com" />
</smtp>
</mailSettings>
</system.net>
This code maybe will help!
string from = me#gmail.com; //Replace this with your own correct Gmail Address
string to = you#gmail.com //Replace this with the Email Address to whom you want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "Password");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage );
} // end try