This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sending email in .NET through Gmail
I don't know what is the apis needed to send emails using gmail and hotmail using .Net , if someone know it will be very useful for me also how to install those libraries into VS 2010
To send emails, use VS 2010 SMTP. You define related parameters in the .config as follows:
<system.net>
<mailSettings>
<smtp from="youremail#yourdomain.com">
<network host="mail.yourdomain.com" port="yourport" userName="youremail#yourdomain.com" password="yourpassword" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
As an example of the C# code to send emails:
public static void Send(string Subject, string From, string Body, List<emailAddress> CC, MailAddress To, bool BodyHTML)
{
try
{
MailMessage mail = new MailMessage();
if (CC != null)
{
foreach (emailAddress ea in CC)
{
mail.CC.Add(new MailAddress(ea.email, ea.fullname));
}
}
mail.Subject = Subject;
mail.Body = Body;
mail.IsBodyHtml = BodyHTML;
mail.From = new MailAddress(From);
mail.To.Add(To);
SmtpClient client = new SmtpClient();
client.Host = "mail.yourdomain.com";
client.Send(mail);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
You're looking for the System.Net.Mail namespace. No external libraries needed - to start just add a using directive to it at the top of your class.
Here's a site with some good references to get you started:
http://www.systemnetmail.com/
Related
I have hosted a website from Godaddy hosting in Windows ASP.net and have purchased an email also from there as well.
On that website, there is a ContactUs Page in which any visitor could fill in his details and Submit.
For this I am internally using my personal gmail id as 'From' and my info email ID as 'To' to send the email.
I am using the sample code they provided on their website as this link.
But I am getting this error:
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code.
Additional information: Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 72.167.234.197 (72.167.234.197:25), connect error 10060
On client.Send(message)
This is my web.config entry as per that link:
<system.net>
<mailSettings>
<smtp from="personalID#gmail.com">
<network host="relay-hosting.secureserver.net" port="25" />
</smtp>
</mailSettings>
</system.net>
And this is my code to send email:
StringBuilder body = new StringBuilder();
body.AppendLine("<h3>New Query Received:</h3>");
body.AppendLine();
body.AppendFormat("Name : {0}<br />", txtName.Text);
body.AppendFormat("Email : {0}<br />", txtEmail.Text);
body.AppendFormat("Phone : {0}<br />", txtMobile.Text);
body.AppendFormat("Query : {0}<br />", txtQuery.Text);
MailMessage message = new MailMessage();
message.From = new MailAddress("manish.rnsconsultancy#gmail.com");
message.To.Add(new MailAddress("info#rns-consultancy.com"));
message.Subject = "New Query from Website";
message.Body = body.ToString();
SmtpClient client = new SmtpClient();
client.Send(message);
Can anyone please help me in this matter, I am stuck on this as their customer support doesn't know other than that link.
I tried switching the emails as From and To also, but got the same error.
try this code to send email.
Send email using System.Net.Mail
To send mail using System.Net.Mail, you need to configure your SMTP service in your application's web.config file using these values for mailSettings:
<system.net>
<mailSettings>
<smtp from="your email address">
<network host="relay-hosting.secureserver.net" port="25" />
</smtp>
</mailSettings>
</system.net>
The network rule's value will be used when you instantiate an SmtpClient in your code.
PS: Please change port 3535 if you have problems with 25
C# code example
You can then use code similar to this to send email from your application:
MailMessage message = new MailMessage();
message.From = new MailAddress("your email address");
message.To.Add(new MailAddress("your recipient"));
message.Subject = "your subject";
message.Body = "content of your email";
SmtpClient client = new SmtpClient();
client.Send(message);
after checking you DNS record i found
smtp.asia.secureserver.net
Use this code and Add your password
var message = new MailMessage();
message.To.Add(new MailAddress("info#rns-consultancy.com"));
message.From = new MailAddress("info#rns-consultancy.com");
message.Subject = "Subject";
message.Body = "Body";
var smtpClient = new SmtpClient();
smtpClient.Host = "smtp.asia.secureserver.net";
smtpClient.Port = 25;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials =
new NetworkCredential("info#rns-consultancy.com", "Password");
//smtpClient.Port = 465;
//smtpClient.EnableSsl = true;
//smtpClient.Timeout = 10000;
// don't use 100000 in production it's 2 match
//, just for testing
smtpClient.Timeout = 100000;
smtpClient.Send(message);
refrences
Google Public DNS
Mail server addresses and ports for Business Email | Workspace Email - GoDaddy Help SG
Network Tools: DNS,IP,Email
Solved: Not able to send email via C#. - GoDaddy Community
What type of hosting account do I have? | GoDaddy Help AE
c# - SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission' - Stack Overflow
I'm trying to create a Button_Click event that sends an email to a gmail account. This is the error I'm getting:
Unable to read data from the transport connection: net_io_connectionclosed.
It's pointing out Line 63 which is:
client.Send(mail);
Here is the code:
protected void Button2_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 465;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress("yourclassroomconnection#gmail.com"));
mail.From = new MailAddress("yourclassroomconnection#gmail.com");
mail.Subject = "New Order";
string bodyTemplate = Label2.Text;
mail.Body = bodyTemplate;
client.Send(mail);
}
Any idea where I'm going wrong?
You can use below code as a small test. Try sending email with minimal option. Then add other options like html support. So you can narrow down the problem when you're experimenting a new thing.
try {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
} catch (Exception ex)
{
}
You need to generate app specific password and use it here instead of your gmail password.
Please read this tutorial also.
http://csharp.net-informations.com/communications/csharp-smtp-mail.htm
Hard coding the username and password (i.e. the credentials) may be sometimes frustrating.
What you can do is, you can add these credentials in web.config file only once. And you are good to go. Here is the better solution.
web.config file code goes as follows:
<configuration>
<appSettings>
<add key="receiverEmail" value ="ReceiverEmailAddressHere"/>
</appSettings>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="yourclassroomconnection#gmail.com">
<network host="smtp.gmail.com" port="587" enableSsl="true"
userName="YourActualUsername" password="YourActualPassword"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
Please note that you have to change the host according to your gmail account. I am not sure whether the host is correct. I am using outlook to send emails so the host would be smtp-mail.outlook.com
This is how your web.config file would have all the necessary connection credentials that you define at one place at a time. You don't have to use it everytime you use the Email functionality in your application.
protected void btnSendMail_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
// get the receiver email address from web.config
msg.To.Add(ConfigurationManager.AppSettings["receiverEmail"]);
// get sender email address path from web.config file
var address = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string emailAddress = address.Network.UserName;
string password = address.Network.Password;
NetworkCredential credential = new NetworkCredential(emailAddress, password);
msg.Subject = " Subject text here "
}
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Send(msg); // send the message
The key point here is to access the sender's email address and receiver's email address. Note that I have used (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp"); This will navigate your web.config file and search through the hierarchy available in it - Grabs the email address, fails if it doesn't get email address.
Hope this helps. Happy coding!
I am trying to setup simple but complete ASP.NET MVC 4 web app, where I can send email to specific address, I configure the web.config file for SMPT settings and code in controller call, but I am getting error message "The SMTP host was not specified"
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="myEmail#hotmail.co.uk">
<network host="smtp.live.com" port="25" userName="myEmail#hotmail.co.uk" password="myPassword" defaultCredentials="true"/>
</smtp>
</mailSettings>
in controller class
var mailMessage = new MailMessage();
mailMessage.To.Add("yourEmail#hotmail.co.uk");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
var smptClient = new SmtpClient { EnableSsl = false };
smptClient.Send(mailMessage);
many thanks
Best Idea to use SMTP mail functionality in .NET + MVC/ASP is this open source codeplex library:
http://higlabo.codeplex.com/
Especially since the default-delivered components in .NET framework does fully support all types of SSL/TSL etc. (implicit/explicit mode as keyword here)
http://higlabo.codeplex.com/
From a quick look you haven't set up the "From" property.
var mailMessage = new MailMessage();
mailMessage.To.Add("yourEmail#hotmail.co.uk");
mailMessage.From = new MailAddress("myEmail#hotmail.co.uk");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
Your code and configuration look correct.
Are you sure you have put the system.net/mailSettings element in the web.config in the root directory of your web site?
A common mistake is to put such settings in the web.config in the Views folder.
Incidentally, the MailMessage class implements IDisposable, as does the SmtpClient class from .NET 4. So you should be enclosing both in using blocks.
Not sure if smtp.live.com is still valid http://windows.microsoft.com/en-ca/windows/outlook/send-receive-from-app does not seem to list it
I would check if Port 25 is Blocked if Port 25 is blocked, try Port 587 (Might have to enable SSL for 587)
you missing smptClient.Send(mailMessage); at the end of your code
var mailMessage = new MailMessage();
mailMessage.To.Add("yourEmail#hotmail.co.uk");
mailMessage.From = new MailAddress("myEmail#hotmail.co.uk");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
//this what you miss
smptClient.Send(mailMessage);
//
do some thing like this
SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtp], Convert.ToInt32(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpport]));
if (ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpUseCredentials] == "true")
{
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpusername], ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtppassword], ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtp]);
}
else
{
smtp.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
if (SendTo.Count == 0)
{
SendTo.Add(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.ToMail]);
}
foreach (string recipientemail in SendTo)
{
oEmail.To.Add(recipientemail);
try
{
smtp.Send(oEmail);
}
catch (Exception)
{
}
oEmail.To.Clear();
}
}
I wrote a blog post about doing this.
http://www.bgsoftfactory.net/5-steps-to-send-email-with-mvcmailer-from-an-mvc-4-0-application/
I took the easier way, using MVCMailer. Even if sending email from MVC is quite easy, it's a little more complicated to make it nice, while MVCMailer allow to use razor templates to format the body of your email.
You may save yourself some time by using MVCMailer.
I have been trying to send an email via C# from a gmail account for account registration for my website.
I have tried several ways however the same exception continues to pop up: System.Net.Mail.Smtp Exception - Connection has timed out.
This is what I inluded in my Web.config file:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network"
from="Writely <mrbk.writely#gmail.com>">
<network host="smtp.gmail.com"
port="465"
enableSsl="true"
defaultCredentials="false"
userName="mrbk.writely#gmail.com"
password="******" />
</smtp>
</mailSettings>
</system.net>
where writely is the name of my website, and mrbk.writely#gmail.com is the account I wish to send an email from.
Then in my Account Controller when I connect with my database and save the user in my table, I am creating my MailMessage object and attempting to same the mail by:
using (DBConnection conn = new DBConnection())
{
conn.UserInfoes.Add(userInfo);
conn.SaveChanges();
MailMessage mail = new MailMessage();
mail.From = new MailAddress("mrbk.writely#gmail.com");
mail.To.Add("bernice.zerafa11#gmail.com");
mail.Subject = "Welcome to Writely";
mail.Body = "Test content";
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
Am I missing something or doing something wrong? I read that this is the good way to do this in some other question on stack overflow so I really don't know what's the problem here.
Thanks for your help :)
You need to tell the SmtpClient what settings to use. It does not automatically read this information from the Web.Config file.
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);
smtp.Credentials = new NetworkCredential("mrbk.writely#gmail.com", "***");
smtp.EnableSsl = true;
smtp.Send(mail);
gmail requires authentication:
Outgoing Mail (SMTP) Server
requires TLS or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
so what i did is
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("mrbk.writely#gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("bernice.zerafa11#gmail.com", "bernice.zerafa11#gmail.com", "Welcome to Writely", "Test content");
I had the exact same problem and it's resolved after switching the port number from 465 to 587.
I had the problem on "email confirmation", "password recovery", and "sending email" and now all 3 problems are resolved :).
I know it's a pretty old post, but I usually use the existing posts to find answers instead of asking for new questions.
Thank you all for all your helps.
As I have already answered here.
This problem can also be caused by a security configuration in you gmail account.
The correct port is 587, but to authenticate you need to allow access from less secure apps in your gmail account.
Try it here
It worked for me, hope it helps..
Example in asp.net web forms/sharepoint
StringBuilder Body = new StringBuilder();
Body.Append("Your text");
String FromEmail = "you email";
String DisplayNameFromEmailMedico = "display when you receive email";
MailMessage message = new MailMessage();
message.From = new MailAddress(FromEmail, DisplayNameFromEmailMedico);
message.To.Add(new MailAddress("myclient#gmail.com"));
message.Subject = "subject that print in email";
message.IsBodyHtml = true;
message.Body = Body.ToString();
SmtpClient client = new SmtpClient();
NetworkCredential myCreds = new NetworkCredential("yoursmtpemail#gmail.com", "key from email smtp", "");
client.EnableSsl = true;
client.Credentials = myCreds;
client.Send(message);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
send email asp.net c#
I have sent mail several times using the technique several times before but somehow it doesnt work i am providing the code in the following:
MailMessage myMailMessage = new MailMessage();
myMailMessage.Subject = "Response From Client";
myMailMessage.Body = "hello word";
myMailMessage.From = new MailAddress("mymail#gmail.com", "jub");
myMailMessage.To.Add(new MailAddress("mymail#gmail.com", "Softden"));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMailMessage);
and my web.config is:
<mailSettings>
<smtp deliveryMethod = "Network" from="Jubair <mymail#gmail.com>">
<network defaultCredentials="false" enableSsl="true" userName="mymail#gmail.com" password="Mypassword" host="smtp.gmail.com" port="587"></network>
</smtp>
</mailSettings>
it says the smtp server requires a secure connection or the client was not authenticated.. Please help
Try adding something like this
Per Dominic's answer on Stackoverflow look at he following example
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
//UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
//----------------- A simple approach below ---------------
I just tested this below and it works
var mail = new MailMessage();
// Set the to and from addresses.
// The from address must be your GMail account
mail.From = new MailAddress("noreplyXYZ#gmail.com");
mail.To.Add(new MailAddress(to));
// Define the message
mail.Subject = subject;
mail.IsBodyHtml = isHtml;
mail.Body = message;
// Create a new Smpt Client using Google's servers
var mailclient = new SmtpClient();
mailclient.Host = "smtp.gmail.com";//ForGmail
mailclient.Port = 587; //ForGmail
// This is the critical part, you must enable SSL
mailclient.EnableSsl = true;//ForGmail
//mailclient.EnableSsl = false;
mailclient.UseDefaultCredentials = true;
// Specify your authentication details
mailclient.Credentials = new System.Net.NetworkCredential("fromAddress#gmail.com", "xxxx123");//ForGmail
mailclient.Send(mail);
mailclient.Dispose();
//The .config settings there are two options on how you could set this up I am suspecting that this is the issue you are having
<system.net>
<mailSettings>
<smtp from="from#gmail.com" deliveryMethod="Network">
<network userName="from#gmail.com" password="mypassword" host="smtp.gmail.com" port="587"/>
</smtp>
</mailSettings>
</system.net>
or option 2
<configuration>
<appSettings>
<add key="smtpClientHost" value="mail.localhost.com"/> //SMTP Client host name
<add key="portNumber" value="587"/>
<add key="fromAddress" value="yourEmailAddress#gmail.com"/>
</appSettings>
</configuration>
Send Email from Yahoo, Gmail, Hotmail (C#)
http://www.codeproject.com/Tips/520998/Send-Email-from-Yahoo-Gmail-Hotmail-Csharp
These are evry good tutorials with samples. make your demo email id. pass your id and its password as parameters. and send mail to anyone.
http://www.codeproject.com/Articles/15807/Easy-SMTP-Mail-Using-ASP-NET-2-0
http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp
http://www.codeproject.com/Tips/490604/Sending-mail-using-ASP-NET-with-optional-parameter
if you are still unable to send email make sure to change the port number. but 587 should work normally.
Make sure you contact the email server side to see what kind of authentication they accept to relay.