Sending mail using C# - c#

I have to send mail using C#. I follow each and every step properly, but I cannot send mail using the code below. Can anybody please help me to solve this issue? I know it's an old issue and I read all the related articles on that site about it. But I cannot solve my issue. So please help me to solve this problem. The error is: Failure sending mail. I use System.Net.Mail to do it.
using System.Net.Mail;
string mailTo = emailTextBox.Text;
string messageFrom = "riad#abc.com";
string mailSubject = subjectTextBox.Text;
string messageBody = messageRichTextBox.Text;
string smtpAddress = "mail.abc.com";
int smtpPort = 25;
string accountName = "riad#abc.com";
string accountPassword = "123";
MailMessage message = new MailMessage(messageFrom, mailTo);
message.Subject = mailSubject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Body = messageBody;
message.BodyEncoding = System.Text.Encoding.UTF8;
SmtpClient objSmtp = new SmtpClient(smtpAddress, smtpPort);
objSmtp.UseDefaultCredentials = false;
NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(accountName, accountPassword);
objSmtp.Credentials = basicAuthenticationInfo;
objSmtp.Send(message);
MessageBox.Show("Mail send properly");

If the target mail server is an IIS SMTP (or indeed any other) server then you'll have to check the relay restrictions on that server.
Typically, you either have to configure the mail server to accept incoming relay from your machine's name (if in Active Directory) or IP address. Either that, or you can make it an Open Relay - but if it's a public mail server then that is not recommended as you'll have spammers relaying through it in no time.
You might also be able to configure the server to accept relayed messages from a particular identity - and if this is website code that'll mean that you will most likely have to configure the site to run as a domain user so that the NetworkCredentials are sent over correctly.

oh friends...i got the solution.
just i used the port 26.now the mail is sending properly.
int smtpPort = 26;
anyway thanks to Zoltan
riad.

Related

Gmail SMTP failure using c# SmtpClient

Our application has the possibility to send emails via SmtpClient. One of our customer is trying to do so by using his gmail account and it fails resulting in a timeout. However, from our testing lab it works just fine (with another gmail account).
In the trace i can see that the server is not answering with Certificate, Server Key Exchange, Server Hello Done. And im wondering what can be the cause for this?
I also noticed in the traces, the customer is using TLSv1 so I tried to replicate the error on a Windows7 system but still it works for me.
oSmtp = new SmtpClient(this.host, this.port);
oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
oSmtp.EnableSsl = ssl;
NetworkCredential oCredential = new NetworkCredential(this.userName, this.password);
oSmtp.Credentials = oCredential;
oSmtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userState = null;
oSmtp.SendAsync(oMsg, userState);
As far as the code goes, enableSsl is true, the port is 587 and we also instructed our customer to check his gmail account and allow less secure applications.
We will ask the customer for more specific details and try to put more traces in our application, but i would like to know if there is anything that can prevent the server to answer with Certificate,...
Inspecting the traces revealed no significant difference between customers Client Hello and our test Client Hello.
Thanks!
This is a working sample i used few days ago:
string fromAddress = "fromaddress#gmail.com";
var toAddress = new MailAddress("fake#email.com", "To person");
const string fromPassword = "pass";
const string subject = "test";
const string body = "Hey now!!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(fromAddress);
mail.To.Add(toAddress);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
//mail.Attachments.Add(new Attachment("C:\\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
also make sure to enable Less secure app access for the gmail you are using to send data:

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

SMTP server can't send emails from hosting c# asp.net

Good day to everyone. I've writen a project based on asp.net mvc3. Part of project is based on sending emails from my application.
public void SendEmail(string address, string subject, string message, int id)
{
string email = "emailname#gmail.com";
string password = "somepassword";
var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
msg.Attachments.Add(new Attachment(Server.MapPath("~/Content/StudentPdf/student" + id + ".pdf")));
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
This code works locally, perfectly sending emails. But when I upload this to the hosting, it causes an error
the SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required.
I've tried to change port to 465, but then it will be get me an tcp_ip error on the hosting. And one more: and when users try to send emails from this mailbox google tell me that suspicious activity on the application. It's because my hosting in one country and I am in another country.
I have no idea what I have to do next. I've tried googling and found something about 2 level registration, but don't understand how I need implement it in my method.
I'm using arvixe hosting. Maybe others have the same problems?
Please login into your gmail account manually and allow the IP of your hosting under gmail settings.Thats why i think its working perfectly on your local.
Same happened with me and after doing this there was no problem.

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

Can for loop be used to send bulk mails, Bulk mail Issues, AWS(amazon) SES

I am using C# and .net for coding, to send bulk mails around 5000 mails at one shot using AWS(AMazon) SES(Simple Email Service) API, Everything is working fine if the number of mails sending are less than 500-600(approximately), but if it is more like 5000 it will send upto 500-600 and then it will stop sending emails. I have used datatable to store the mails list from database, assigned template body and subject, and then used for loop to send emails one by one. I need to know whether it is coding problem or for loop issue or some other thing ? Any suggestion is also helpful to me?
for (i = 0; i < dtable1.Rows.Count; i++)
{
Object a=dtable1.Rows[i]["student_emailid"];
String TO1=Convert.ToString(a);
TO1 = TO1.Trim();
if (TO1.Equals("")) {
continue;
}
const String FROM = "asit#amcsquare.com"; // Replace with your "From" address. This address must be verified.
String TO = TO1; // Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
String SUBJECT = email_subject;
String BODY = templateBody;
// Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
const String SMTP_USERNAME = "XXXXXXXXXXXX"; // Replace with your SMTP username.
const String SMTP_PASSWORD = "XXXXXXXXXXXXXXXXXX"; // Replace with your SMTP password.
// Amazon SES SMTP host name. This example uses the us-west-2 region.
const String HOST = "email-smtp.us-east-1.amazonaws.com";
// Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use
// STARTTLS to encrypt the connection.
const int PORT = 587;
// Create an SMTP client with the specified host name and port.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
// Create a network credential with your SMTP user name and password.
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
//Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then
//the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
client.EnableSsl = true;
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.Send(message1);
}
}
I don't know the exact reason why it is working now, but it's working. I changed the logic of the above code, it started working. Instead of fetching the SMTP connection each time, for sending each mail previously, this time I fetched the smtp connection only once and used it to send all the bulk mails at once and it started working.But the problem is the sending time, it is taking too much to send all the mails.Anyways I will find the solution for this also.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
client.EnableSsl = true;
for(i=0;i<mail.Count;i++)
{
String TO = mail[i];
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message1);
}
client.Dispose();
}
Label1.Text = mail.Count.ToString() + " mails sent !!";
}

Categories