SMTP relay - send email from console application - c#

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.

Related

Access denied when sending a mail from C# programm

I'm developing a third-party add-on to run in a program called M-Files.
The purpose of the add-on is to send a mail with the help of an SMTP server. I created a fake SMTP server in DevelMail.com just for testing.
Testing the SMTP server from a browser works but when i run the code it gives me the following error.
Transaction failed. The server response was: 5.7.1 Client host rejected: Access denied
Here are the SMTP information:
Host: smtp.develmail.com
SMTP Port: 25
TLS/SSL Port: 465
STARTTLS Port : 587
Auth types: LOGIN, CRAM-MD5
Here is the code:
MailAddress adressFrom = new MailAddress("notification#mfiles.no", "M-Files Notification Add-on");
MailAddress adressTo = new MailAddress("majdnakhleh#live.no");
MailMessage message = new MailMessage(adressFrom, adressTo);
message.Subject = "M-Files Add-on running";
string htmlString = #"<html>
<body>
<p> Dear customer</p>
<p> This is a notification sent to you by using a mailadress written in a metadata property!.</p>
<p> Sincerely,<br>- M-Files</br></p>
</body>
</html>
";
message.Body = htmlString;
SmtpClient client = new SmtpClient();
client.Host = "smtp.develmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword");
client.EnableSsl = true;
client.Send(message);
Reason for the Issue:
Usually, email sending option using SMTP encountered Access denied
because there should have a sender email which required to allow
remote access. When SMTP request sent from the sender email
it checks whether there is remote access allowed. If no, then you
always got Access denied message.
Solution:
For example let's say, you want to send email using Gmail SMTP in that case you do have to enable Allow less secure apps: ON
How To Set
You can simply browse this link Less secure app access and turn that to ON
See the screen shot
Code Snippet:
public static object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
MailMessage mail = new MailMessage();
//Must be change before using other than gmail smtp
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);//Enter the credentails from you have configured earlier
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Note: Make sure, fromEmail and (senderName, senderPass) should be same email with the credential.
Hope that would help.

How do I send an Email using c#?

I am trying to make a Windows Form that sends an email when the user clicks a button, but every time I try, an exception is being raised: Failure sending mail. What is wrong with my code?
private void button1_Click(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("username", "password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("myemailadress#gmail.com");
smtpClient.Host = "smpt.gmail.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("towhomisend#yahoo.com");
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Error, could not send the message
MessageBox.Show(ex.Message);
}
}
use "smtp.gmail.com" instead of "smpt.gmail.com"
1) Check whether SMTP settings are correct and server is configured correctly (Host & Port Settings)
2)Check whether credentials(user name and password) are correct
3)Check whether firewall is blocking the request
4).Check port 587 If it is blocked in firewall
Port 587:
This is the default mail submission port. When a mail client or server is submitting an email to be routed by a proper mail server, it should always use this port.
The 'smtpClient.Host = "smpt.gmail.com";' part is incorrect.
Change "smpt.gmail.com" to "smtp.gmail.com"
"S M T P" - not - "S M P T"
use "smtp.gmail.com" instead of "smpt.gmail.com"
That aside
Confirm one more thing, You have to enable email from other program in your gmail.
When you try to send once, you will get a notification in you email ID.
You have to enable it there.

C# sending email code suddenly stopped working

i have the following code that has worked fine for months but suddently stopped working on this line:
smtpClient.Send(msg);
this is an internal application sending emails to internal people in my company
with the following error:
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for ABC#comp.com
can anyone think of a reason why this code would work fine for ages and suddently just stop work ?
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromEmailAddress_);
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress(email.Trim()));
msg.Subject = subject_;
msg.Body = body_;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
var smtpClient = new SmtpClient(_mailServer);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(_user, _pwd);
try
{
smtpClient.Send(msg);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
The mail server you're connecting to has been tightened up to not allow relaying either for your credentials or from your network/IP. This is not a code issue (short of changing the SMTP server) but an issue or question for whoever manages the SMTP server you're using.
Apart from checking to ensure the smtp service is running you can try the following:
Try setting the SmtpDeliveryMethod enum to PickupDirectoryFromIis.
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpdeliverymethod.aspx

c# Sending emails with authentication. standard approach not working

I am trying to send an email using the following very standard code. However, I get the error that follow...
MailMessage message = new MailMessage();
message.Sender = new MailAddress("sen#der.com");
message.To.Add("reci#pient.com");
message.Subject = "test subject";
message.Body = "test body";
SmtpClient client = new SmtpClient();
client.Host = "mail.myhost.com";
//client.Port = 587;
NetworkCredential cred = new NetworkCredential();
cred.UserName = "sen#der.com";
cred.Password = "correct password";
cred.Domain = "mail.myhost.com";
client.Credentials = cred;
client.UseDefaultCredentials = false;
client.Send(message);
Mailbox unavailable. The server
response was: No such
user here.
This recipient email address definitely works. To make this account work I had to do some special steps in outlook. Specifically, I had to do change account settings -> more settings -> outgoing server -> my outgoing server requires authentication & use same settings. I am wondering if there is some other strategy.
I think the key here is that my host is Server Intellect and I know that some people on here use them so hopefully someone else has been able to get through this. I did talk to support but they said with coding issues I am on my own :o
try this...
NetworkCredential cred = new NetworkCredential();
cred.UserName = "sen#der.com";
cred.Password = "correct password";
//cred.Domain = "mail.myhost.com";
... you should not need to provide the .Domain unless you are using Kerberos or some other complex authentication.
Edit...
Check out my extended answer. It has an example of how to send an email with authentication. It's also has SSL enabled so you may need to remove that part.
there is no mailbox called sen#der.com on server mail.myhost.com, check that

Sending email using .NET

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;

Categories