Sending an email from c# windows application - c#

I have a windows c# application which is running on server, After it has been run everyday through task scheduler at certain time it generates a log file and now my issue can i send a email to my office id after it has finished running the application where there is no from address and needs to attach the log file with the email.
private void button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage("eg#king.co.uk", "user#hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = #"100.100.0.1";- fake host
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
}

Many issues in your code and question.
Your question says "how do I send an email to my office id". It's not clear what that means. Do you mean you have a company email address such as shruti#mycompany.com and you want to send an email to it?
There must be a from address. The address doesn't have to exist. For example, you would have a from address of donotreply#mycompany.com.
Your question includes the code for sending an email via Gmail's servers: client.Host = "smtp.google.com";. This makes everyone that is reading your question think you're trying to send an email via Google, which has specific requirements. If that's not the case, then update your question to be explicit. For example, if you're using a company hosted email server, you can use smtp.mycompany.com.
So if the question is really about how to attach a text file to your email, here's how you'd do that:
private void button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage("donotreply#yourcompany.com", "user#hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "smtp.mycompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
mail.Attachments.Add(new Attachment("log.txt"));
client.Send(mail);
}
Notice I removed the UseDefaultCredentials=false because you didn't specify alternate credentials.

If you know the format or name of the daily log message, you can use the following code after you create your MailMessage:
MailMessage mail = new MailMessage("you#yourcompany.com", "user#hotmail.com");
mail.Attachments.Add(new Attachment("filepath.log"));

Related

Send email through smtp in c# through more than one from email addresses and to more than one receiver

I have project related school management system in c# using asp.net as framework and sql server as database.
I need to send email with same email body and subject (like: wish you a very happy new year) but i have different from addresses and obviously different to addresses.
For example: i need to send email to all teachers with aa#aa.aa email and to all students with bb#bb.bb email address and to management staff with cc#cc.cc
How can i perform this task in c# and asp.net with efficient way?
You can create SmtpClient and MailMessage object in order to send email.
I think you should call your mail sending method with 3 times with different parameters (from address and receivers list) Your methos looks like
SendMail("aa#aa.aa", teacherList);
SendMail("bb#bb.bb", studentList);
SendMail("cc#cc.cc", staffList);
public static void SendMail(string fromAddress, List<string> emailAddresses)
{
//make this smtpClient global. Because you will use next time.
SmtpClient smtpClient = new SmtpClient();
smtpClient.Credentials = new System.Net.NetworkCredential("smtpUserName", "smtpPassword");
smtpClient.Host = "mail.mailhost.com";//set your smtp host. Generally mail.domain.com
smtpClient.Port = 587;//set your smtp port
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;//you can change this based on your settings
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromAddress, "Our School Name");
for (int i = 0; i < emaillAddresses.Count; i++)
{
mail.Bcc.Add(new MailAddress(emaillAddresses[i]));
}
mail.Subject = "Wish you a very happy new year";
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.UTF8;
smtpClient.Send(mail);
}

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:

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# 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
}

Categories