I want to create a button and when user click on that, a window form will open and the From is default text, "To" is also load from code behind and user can edit that text, "Content" is default text and user can edit too.
So now I can send email with:
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient("gw1.scei.a-star.edu.sg");
mail.From = new MailAddress("mydefaultemail");
mail.To.Add("the To emails will be input here");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail.";
SmtpServer.Credentials = new System.Net.NetworkCredential("mydefaultemail", "");
SmtpServer.Send(mail);
Now I don't know how could I make it to be wildows form and catch the text in that form to input into this code?
You add a textbox to your Windows form.
Then, in your code, you get the text property value of that textbox and set your email variable accordingly.
mail.Body = myTextBox.Text;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(
"yourid#gmail.com", "yourgmailpassword");
MailMessage msg = new MailMessage();
msg.To.Add("Send To email Id");
msg.From = new MailAddress("yourid#gmail.com");
msg.Subject ="Subject";
msg.Body = "Message";
client.Send(msg);
Related
I'm trying to send an e-mail using C#, but I get this recurring error :
.
Can you explain me what's wrong with my code ?
Here it is :
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("myEmailAdress#gmail.com", "myPassword");
MailMessage msg = new MailMessage();
msg.To.Add("receiver#gmail.com");
msg.From = new MailAddress("sender#gmail.com");
msg.Subject = "My subject";
msg.Body = "My body";
client.Send(msg);
MessageBox.Show("Message sent !");
I have encountered same before.
You are getting this error because you haven't set ON on Less secure app access for sender#gmail.com as you are using Gmail SMTP port.
Reason:
Your email has no remotely access permission. You have to configure
it. Suppose you want to sent email from sender#gmail.com so you
have set that permission NO to this account.
How To Set:
You could try like below
Or can open that tab from this link directly Less secure app access
Update:
As per your comment this is for you which has working perfectly since the beginning of my career
public 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
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;
// mail.Attachments.Add(new Attachment(#attacmmentLocationPath));
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Hope that would help.
This code works for gmail, it is very similar to yours with slight differences, but if you try this, and it doesn't work for you, the issue is not the code - perhaps some other network related issue that you are going to need to fix first:
using (var msg = new MailMessage())
{
msg.From = new MailAddress("fromaddress#gmail.com");
msg.To.Add("toaddress#gmail.com");
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("xxx#gmail.com","password");
smtp.Send(msg);
}
}
An SMTP client may log in using an authentication mechanism chosen among those supported by the SMTP servers.
I tried to send an email and set display name of the from Email address. But when i received email in my client for instance gmail. Instead of display name i see email address without domain means From Email "FromEmail#abc.com" then when i received it shows me i received email from "FromEmail".
Here is my code
MailMessage mail = new MailMessage();
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.HeadersEncoding = System.Text.Encoding.UTF8;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Priority = System.Net.Mail.MailPriority.High;
mail.IsBodyHtml = true;
mail.From = new MailAddress("FromEmail#abc.com","Automate");
mail.To.Add(new MailAddress("ToEmail#abc.com"));
mail.Subject = "This is test email";
mail.Body = "This is test mail.";
SmtpClient client = new SmtpClient("smtp.office365.com");
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("senderEmail#abc.com", "*****");
client.Send(mail);
I have to send a mail on form submit but it does'nt work with me
// envoyer mail
SmtpClient client = new SmtpClient("host");
//User name and password goes here
MailMessage mailMessage = new MailMessage { From = new MailAddress("someone#somewhere.com") };
mailMessage.To.Add("someone#somewhere.com");
mailMessage.Subject = "Hello There";
mailMessage.Body = "Hello my friend!";
client.Host = "host";
client.Port = 25;
User name and password
client.EnableSsl = false;
client.Send(mailMessage);
Thank you
I'm trying to send a password reset email, but I'm having trouble figuring out how to specify the sender's address.
Here's what I'm trying to do:
MailMessage mail = new MailMessage();
mail.From.Address = "support#mycompany.com";
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "Click here to reset your password.";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
I'm sure it's possible, so how can I accomplish this in ASP.Net?
It turns out I was getting ahead of myself.
Removing Address from mail.From.Address allowed me to set the value, but needed the type MailAddress.
Here's the solution:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("support#mycompany.com");
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "Click here to reset your password.";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
I'm trying to send a email in C# using Gmail. I want the 'from' header to have another my own specified email address whenever user receive email. Could anyone please tell me how can I do this?
MailMessage mailMsg = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(username, password);
MailAddress mailAdd = new MailAddress("jack2#gmail.com");
mailMsg.Sender = new MailAddress(username);
mailMsg.From = mailAdd;
//mailMsg.Headers.Add("Sender",username);
mailMsg.Bcc.Add(builder.ToString());
mailMsg.Subject = txtSubject.Text;
mailMsg.Body = txtBody.Text;
mailMsg.IsBodyHtml = chkHtmlBody.Checked;
if (System.IO.File.Exists(txtAttechments.Text))
{
System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text);
mailMsg.Attachments.Add(attechment);
}
client.Send(mailMsg);
In above code 'username' and 'password' fields contain another email address and password. The received email having 'from' header with value
TRY this if your mail provide is not gmail and also not using IMAP services.
MailMessage mailMsg = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "mail.youdomain.com"; //////EDITED
client.EnableSsl = false; //////EDITED
client.Credentials = new System.Net.NetworkCredential(username, password);
MailAddress mailAdd = new MailAddress("jack2#gmail.com");
mailMsg.Sender = new MailAddress(username);
mailMsg.From = mailAdd;
//mailMsg.Headers.Add("Sender",username);
mailMsg.Bcc.Add(builder.ToString());
mailMsg.Subject = txtSubject.Text;
mailMsg.Body = txtBody.Text;
mailMsg.IsBodyHtml = chkHtmlBody.Checked;
if (System.IO.File.Exists(txtAttechments.Text))
{
System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text);
mailMsg.Attachments.Add(attechment);
}
client.Send(mailMsg);