-
April 29th, 2009, 06:44 AM
#1
Sending E-mails in .NET!?
Hi, guys.
I'm trying to send e-mails from my windows app, with a series of frustrations:
1) found system::net::mail.
-Got excited at first, but the problem is I need an SMTP host
2) my webhost, yahoo, does SMTP!
-problem is a) it says to use SSL and I don't know how in .NET; b) every username/password combo says Yahoo is disconnecting me, so apparently SSL is required, not optional?
3) looked for "free" smtp servers
-they all send their own message with the mail, which looks unprofessional. Also most use SSL, which I don't know how to use.
Can anyone give me a clear and simple way to get some e-mails delivered from my app, or even give me some hints where/how to start finding info on my own? Ideally, does anyone know of a tutorial? Any ideas at all (even helpful guesses) would be GREATLY appreciated.
-
April 29th, 2009, 06:49 AM
#2
Re: Sending E-mails in .NET!?
-
May 24th, 2009, 04:06 PM
#3
Re: Sending E-mails in .NET!?
Hi,
Example code for using SMTP can be found in MSDN, see
SmtpClient Class
To use SSL, check example from here:
SmtpClient.EnableSsl Property
Last edited by Loldemar; May 24th, 2009 at 04:06 PM.
Reason: fixed typo
-
May 25th, 2009, 02:59 PM
#4
Re: Sending E-mails in .NET!?
Okay I decided to do a C# code example using GMail account, and decided to make a new post for that for clarity reasons (sorry if this is considered spamming). About the bennyboy2's question about how to use SSL, it's quite easy. SmtpClient has a property EnableSsl, which you need to set to true. GMail needs to have your username and password, and those you can set in NetworkCredential object , and pass that to SmtpClient's Credentials-property.
Below is a very simple sample that sends a test email message. It's very basic and doesn't do anything fancy, but it will get you started anyway.
Code:
using System;
using System.Net.Mail;
using System.Net;
namespace SmtpTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sending test message...");
// call SendTestMessage-method, with GMail server and port as parameters
SendTestMessage("smtp.gmail.com", 587);
Console.WriteLine("Message sent succesfully!");
Console.ReadLine();
}
public static void SendTestMessage(string server, int port)
{
// message info
string to = "*****.*****@gmail.com"; // put recipient
string from = "*****.*****@gmail.com"; // put sender email here
string subject = "SMTP client using GMail."; // message subject
string body = "This is our test message, w00t!"; // message body
MailMessage message = new MailMessage(from, to, subject, body);
// Initialize client
SmtpClient client = new SmtpClient(server, port);
// enable SSL
client.EnableSsl = true;
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
// Here we define our network credetials (username and password)
NetworkCredential SmtpUserInfo = new NetworkCredential("your.username@gmail.com", "password");
// set the credentials
client.Credentials = SmtpUserInfo;
try
{
// send message
client.Send(message);
}
catch (SmtpException se)
{
Console.WriteLine("SmtpException caught in SendTestMessage(): {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception caught in SendTestMessage(): {0}", e.ToString());
}
}
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|