Sending email using .NET - c#

I have the following code but I am getting an exception that a smtp host is not defined. If I am running this and testing on my local machine from visual studio, what do I need to do to be able to send email from my machine. Do I have to turn on some Windows service?
private void SendMailToAdminToApprove(string email_, string name_)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("address#domain.com", "Person's Name");
msg.To.Add(new MailAddress("a#gmail.com", "Adam"));
msg.Subject = "Message Subject";
msg.Body = "Mail body content";
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
try
{
SmtpClient c = new SmtpClient();
c.Send(msg);
}
catch (Exception ex)
{
Console.Write("T");
}
}

You need to set the SMTP host to point to an actual SMTP server. One option is to run the SMTP service on your own machine, but you could also point to your ISP's server.
edit
As pcampbell and Skeolan mentioned, the actual value should go into app.config. I'm not sure if localhost would be an exception: it would depend on whether you want the option of not running a local server.

You'll need to specify the SMTP host here:
string smtpHost = "localhost";
//or go to your config file
smtpHost = ConfigurationManager.AppSettings["MySmtpHost"].ToString();
SmtpClient c = new SmtpClient(smtpHost);

You need to define the SMTP relay:
SmtpClient c = new SmtpClient("relay.yourdomain.com");
or if you're running the relay locally:
SmtpClient c = new SmtpClient("localhost");

You should change this section:
SmtpClient c = new SmtpClient();
// Either specify a SMTP server above, or set c.Host
c.Send(msg);
You need to specify which SMTP server is going to be used for sending this message. If you install a SMTP server locally, this could be localhost - but otherwise, you'll need to have an outgoing mail server set appropriately.

Here is the code I use for sending email with C#. I also commented out code for sending it to a file locally if you need it.
SmtpClient smtp = new SmtpClient(smtpServer, portNumber);
// Disable SSL when saving to directory.
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(mailFrom, password);
// Set mail to be delivered to a folder
//smtp.PickupDirectoryLocation = #"C:\mail\Send";
//smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;

Related

C# Sending Email using SMTP E-mail

I'm rather new to SMTP and IIS settings but according to the documentation on I've read the web this should be working.
What I'm trying to achieve:
To send an email from the server to a users email using an existing SMTP Relay Server.
What I have done:
In my IIS, for my site (ASP.NET), I have configured the SMTP E-mail.
I have entered:
A random E-mail address (it doesn't have to be an existing, right?)
A SMTP Server IP (in this case an IP to an external SMTP Relay Server)
A port number (25).
Autentication Settings to "Not required".
My method for sending an email looks like this:
public static void SendEmail()
{
var message = new MailMessage()
{
Subject = "Heading",
Body = "Body",
message.From = new MailAddress("test#test.com");
message.To.Add("A valid email address"); //My own email address
}
var smtpClient = new SmtpClient("SMTP-Relay-Server-IP", 25); //Same IP as the one in SMTP E-mail configuration in IIS for the site.
smtpClient.Send(message);
}
}
Facts/questions:
Is this correct? Is it correct to also put the Relay Server IP and Port number in the code as parameters in the new SmtpClient?
I don't get an error but I don't receive an email. (I am 100% sure that the "to-email" is correct.
What can be the reason for this not working? What am I missing or misunderstanding?
Wrap your smtpClient.Send(message); in a try/catch block and log any exceptions that are thrown.
A random E-mail address (it doesn't have to be an existing, right?)
That depends on your SMTP provider and configuration.
Without more information on your SMTP provider or an error message I doubt there's anything we can do for you.
MailMessage mail = new MailMessage("sendTo", "from");
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential("user", "pass");
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
mail.Body = "this my message";
smtp.Send(mail);
should be set out like this

C# Mail Function not connecting to remote server

I am developing a program which needs to have the capability to send emails. I've got a simple mail function setup however I have never really delved into the mail side of things and am not sure if I'm using the correct settings.
I am sure I am doing something wrong with the SMTP, I have set the MailMessage host as the outgoing mail server that I use for emailing from outlook (the email accounts are hosted on shared virtual hosting so I use their supplied hostname in the function) alongside the login credentials I would normally use. When I try to send a test email it throws an unable to connect to remote server exception. I have WAMPSERVER setup on the computer I am trying to run this from, I know it has some kind of SMTP capability? Should I be using this or is there no reason I can't use shared virtual hosting SMTP as the host? Please refer to code below-
public void EmailTracking()
{
string to = "johnsmith#xxxxxxxxxxxxx.com.au";
string body = "this is some text for body";
string subject = "subject line";
string fromAddress = "kelvie#xxxxxxxxxx.com.au";
string fromDisplay = "Kelvie";
string credentialUser = "removed";
string credentialPassword = "removed";
string host = "removed";
MailMessage mail = new MailMessage();
mail.Body = body;
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(to));
mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
smtp.Host = host;
smtp.Send(mail); //fails here with unable to connect to remote server
}

.NET SMTP mail - error = helo command rejected need fully-qualified hostname

I am trying to send email in a .NET console application. I have an SMTP server with i.P. address X.X.X.X (sanitized to protect the innocent).
The SMTP server has been set up (the relay configured) to allow email from the server that is hosting the .NET console app, and send that email to the outside world.
I have tested this with telnet from the server hosting the console app. I successfully sent an email with telnet console from the server hosting the console app using the SMTP server's I.P. address. There is no authentication required when using telnet. I have not been given any connection credentials.
But when I try to do it in the .NET app, I get the following error:
Syntaz error, command unrecognized. The server response was: : Helo command rejected: need fully-qualified hostname
Here is my code:
string mailMessagetest = "test";
string subjecttest = "test";
List<string> recipienttest = new List<string>();
recipienttest.Add("me#mydomain.com");
utility.SendMail(recipienttest, subjecttest, mailMessagetest);
Here is the function SendMail:
public static void SendMail(List<string> recipient, string subject, string message)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("myfriend#mydomain.com");
foreach (string to in recipient)
{
mailMessage.To.Add(to);
}
mailMessage.Subject = subject;
mailMessage.Body = message;
SmtpClient client = new SmtpClient();
client.Host = "X.X.X.X";
client.Port = 25;
client.Send(mailMessage);
}
Change the hostname from an IP address to its name, e.g. "smtp.provider.com" or if its internal "mailserver.domain".
UPDATE:
Try read this system.net.mail.smtpclient fqdn required and this the FQDN is not sent when you send a HELO or EHLO.
Create a user with password on your SMTP server then apply the following settings:
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("user", "pass");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Port = 25;
smtp.EnableSsl = true; // or false depending your server

SMTP relay - send email from console application

Is it possible to send email from a server that uses smtp-relay through a .net application.
I'm using app.config to get the actual values ex server IP, and the fromadress that the email should use.
According to the IT-technician the username and password to authorize should not be needed because it uses smtp-relay. The computer that are going to send the email is on the smtp-servers list of valid computers.
Can this actually work, don't I need to specify the username/pwd?
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(_smtpserver);
mail.From = new MailAddress(_fromAdress);
mail.To.Add(_toAdress);
mail.Subject = _subject;
mail.Body = _body;
mail.Priority = MailPriority.High;
SmtpServer.Port = Convert.ToInt32(_port);
SmtpServer.Credentials = new System.Net.NetworkCredential(_authUsername, _authPassword);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
In your snippet you are specifying credentials
SmtpServer.Credentials = new System.Net.NetworkCredential(_authUsername, _authPassword);
You can remove that line and it still will work if SMTP relay is configured as you said.
in your case,if you are seting your username and password in webconfig even then you need to call it like above.
but if you using your default mail credentials then it will pick automatically..
and if you are using diiferent mail client for sender then you have to pass credentials..
I had same situation. I got same error code. I noticed , I had used system.web.mail. But my application is a console application. I changed library, I used system.net.mail. And it works now.

SMTP Service not available

I am trying to create a web application which upon entering your email address and message , sends an email with this information from the email address.
I used this:
try
{
NetworkCredential login = new NetworkCredential("your_____#gmail.com", "password");
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.To.Add(new MailAddress("my____email#gmail.com"));
email.From = new MailAddress("your_____#gmail.com");
email.Subject = "Question";
email.Body = question;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = login;
client.Send(email);
}
catch
{
}
But its giving me an SMTP error.
"Service not available, closing
transmission channel. The server
response was: Cannot connect to SMTP
server 209.85.129.111
(209.85.129.111:25), connect error
10051" System.Exception
{System.Net.Mail.SmtpException}
To send through your gmail account, you need to connect to port 587:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
You do not need to specify port 587 - the code works without it. I have successfully sent and received e-mail using:
SmtpClient client = new SmtpClient("smtp.gmail.com");
If you look at the error closely, it says "Cannot connect to SMTP server" and error 10051 means the network is unreachable. Do you have a firewall blocking port 587?
Gmail uses port 465 and the erros show port 25
try using 465 port
http://mail.google.com/support/bin/answer.py?answer=76147

Categories