I am trying to test sending out emails to users from a generic email such as noreply#company.com.
I want to do this in a button click event. How do I achieve this without using credentials?
I have tried several things but I always get errors.
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
mm.To.Add(new System.Net.Mail.MailAddress("Email Address", "Name"));
mm.From = new System.Net.Mail.MailAddress("Email Address");
mm.Sender = new System.Net.Mail.MailAddress("Email Address", "Name");
mm.Subject = "This is Test Email";
mm.Body = "<h3>This is Testing SMTP Mail Send By Me</h3>";
mm.IsBodyHtml = true;
mm.Priority = System.Net.Mail.MailPriority.High; // Set Priority to sending mail
System.Net.Mail.SmtpClient smtCliend = new System.Net.Mail.SmtpClient();
smtCliend.Host = "Your smtp server";
smtCliend.Port = 25; // SMTP port no
smtCliend.Credentials = new NetworkCredential("User Name", "Password");
smtCliend.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
smtCliend.Send(mm);
}
catch (System.Net.Mail.SmtpException ex)
{
lblMsg.Text = ex.ToString();
}
catch (Exception exe)
{
lblMsg.Text = "\n\n\n" + exe.ToString();
}
Thanks
Related
I am trying to send an email with Asp.Net but the email will not always be sent using the same WIFI credentials.
string email = "noreply#sender.com";
string subject = "Subject";
string MailContent = "This is the content";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add(email);
mail.From = new MailAddress("noreplay#reciever.com");
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = MailContent;
SmtpServer.Host = "smtpserver";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Exception Message: " + ex.Message);
if (ex.InnerException != null)
MessageBox.Show("Exception Inner: " + ex.InnerException);
}
I tried using this code, which was found on a different question but still can't seem to get an error.
Any alternative can work, I have a pdf which would like to be sent on an email or even one drive if possible. Thanks
I created a registration form in the asp.net and everything is working fine, then, I added the following code to be able to send an email to the user and the host, it is still working but I didn't received any email, do you know what is the problem with my code?
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com",587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("##########", "###########");
MailMessage mailmsg = new MailMessage();
mailmsg.To.Add(TxtEmail.Text);
mailmsg.From = new MailAddress("################");
mailmsg.Subject = "The email is confirmed";
mailmsg.Body = "Dear "+ TxtName.Text + ",\n\nyou have been registred successfully";
client.Send(mailmsg);
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('message has been sent successfully');", true);
MailMessage mailmsg2 = new MailMessage();
mailmsg2.To.Add("################");
mailmsg.From = new MailAddress("##############");
mailmsg.Subject = "New user has registred";
mailmsg.Body = "Dear Admin, \n\nthere are a new user registred in the website";
client.Send(mailmsg);
}
catch (Exception ex)
{
Response.Write("Couldn't send email: " + ex.Message);
}
}
I am getting the output:
Couldn't send email: Failure sending mail
i developed a simple website that stores in the user name and sends that to my emailid and then downloads a file.File is getting downloaded but not mailing me the username.
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("mygmailid");
mailMessage.From =new MailAddress("mydomainbasedemailid");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient=new SmtpClient("mail.mydomain.com",587);
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
Try to set the Smtp.Credentials like this:
string HOSTLOGIN = "YourHostLogin";
string HOSTPW = "YourTopSecretPasswort";
var credentials =
new System.Net.NetworkCredential() { UserName = HOSTLOGIN, Password = HOSTPW };
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
client.EnableSsl = true;
This question already has answers here:
Sending an email to the local domain
(3 answers)
Closed 4 years ago.
I am trying to figure out why I can't send a simple mail with attachment using a office365 account, I don't if my code has something wrong or configuration from account is missing, please help with the code:
code is:
protected void sentmail(string sb)
{
using (var msg = new MailMessage())
{
msg.To.Add(new MailAddress("myaccount#company.cl));
msg.From = new MailAddress("botmail#company.cl");
msg.ReplyToList.Add("botmail#company.cl");
msg.Subject = "Test Mail";
msg.Body = sb.ToString();
msg.IsBodyHtml = true;
var client = new SmtpClient
{
Host = "smtp.office365.com", //it worked on testmail
Credentials = new System.Net.NetworkCredential("botmail#company.cl, "password"),
Port = 25,
EnableSsl = true
};
client.Send(msg);
}
}
Error on sending is:
5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [SC1PR80CA0082.lamprd80.prod.outlook.com]
This is production code that I have in place that will do the sending for you
using (var msg = new MailMessage())
{
msg.To.Add(new MailAddress(userName));
msg.From = new MailAddress(userName);
msg.ReplyToList.Add(model.EmailAddress);
msg.Subject = "Message from the Web";
msg.Body = sb.ToString();
msg.IsBodyHtml = true;
var client = new SmtpClient
{
Host = "xxxmydomainxxx-co-uk.mail.protection.outlook.com",
Credentials = new System.Net.NetworkCredential(userName, password),
Port = 25,
EnableSsl = true
};
// You can use Port 25 if 587 is blocked
try
{
client.Send(msg);
return true;
}
catch (SmtpException smtpEx)
{
return smtpEx;
}
catch (Exception ex)
{
return ex;
}
}
It works without attachment?
Try add the host in credentials:
credentials = new System.Net.NetworkCredential(MAIL_CREDENTIALS_EMAIL, PASS_CREDENTIALS_EMAIL, HOST_EMAIL);
Hi I need to send an email using template in c# windows application . I had created a template but i am unable to pass the parameters through the html template. Here is the template which i am using.
For this HTML Template i am calling this in my windows application and sending through gmail smtp. I am able to send the mail but unable to pass the parameters into it. Please Help me out.Here is the code where i am calling in my windows application
try
{
using (StreamReader reader = File.OpenText("H:\\Visitor Management_Project\\Visitor Management_Project\\Visitor Management_Project\\EmailTemplate.htm"))
{
SmtpClient SmtpServer = new SmtpClient("173.194.67.108", 587);
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Credentials = new System.Net.NetworkCredential("ambarishkesavarapu#gmail.com", "*******");
//SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
message = new MailMessage();
message.Subject = "Visitor Arrived";
//message.SubjectEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
message.Body = "EmailTemplate.htm";
//message.BodyEncoding = System.Text.Encoding.UTF8;
message.From = new MailAddress("ambarishkesavarapu#gmail.com");
message.To.Add(lblCPEmail.Text);
message.Priority = MailPriority.High;
message.Body = reader.ReadToEnd();
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpServer.Send(message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
How to add Parameters into HTML Template where i am getting all the parameters of that in the same page in Textboxes. Please help me out
I think you already discovered the answer yourself, but I will keep posting the answer instead.
In case you are using Windows Forms and not Windows Presentation Forms (The different only the design part and many new features that does not have on Windows Forms), what I have done is like this (to send email):
public void SendEmail(string _from, string _fromDisplayName, string _to, string _toDisplayName, string _subject, string _body, string _password)
{
try
{
SmtpClient _smtp = new SmtpClient();
MailMessage _message = new MailMessage();
_message.From = new MailAddress(_from, _fromDisplayName); // Your email address and your full name
_message.To.Add(new MailAddress(_to, _toDisplayName)); // The recipient email address and the recipient full name // Cannot be edited
_message.Subject = _subject; // The subject of the email
_message.Body = _body; // The body of the email
_smtp.Port = 587; // Google mail port
_smtp.Host = "smtp.gmail.com"; // Google mail address
_smtp.EnableSsl = true;
_smtp.UseDefaultCredentials = false;
_smtp.Credentials = new NetworkCredential(_from, _password); // Login the gmail using your email address and password
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtp.Send(_message);
ShowMessageBox("Your message has been successfully sent.", "Success", 2);
}
catch (Exception ex)
{
ShowMessageBox("Message : " + ex.Message + "\n\nEither your e-mail or password incorrect. (Are you using Gmail account?)", "Error", 1);
}
}
And I am using it like this:
SendEmail(textBox2.Text, textBox5.Text, textBox3.Text, "YOUR_FULL_NAME", textBox4.Text, textBox6.Text, "YOUR_EMAIL_PASSWORD");
Here is the image:
May this answer would help you.
Cheers!