How to send data trough email - c#

I need to send list box items to email. Code is running fine, but when it gets to Send method it fails to send it.
protected void ProcessButton_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("someone#live.com");
mailMessage.From = new MailAddress("myAdress#live.com");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = OrderListBox.Items.ToString();
SmtpClient smtpClient = new SmtpClient("smtp.live.com");
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send email - error: " + ex.Message);
}
}

You can write list in file and send it as attachment (gmail example):
protected bool ProcessButton_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential("myEmail#gmail.com", "password");
//if you have double verification on gmail, then generate and write App Password
client.EnableSsl = true;
MailMessage message = new MailMessage(new MailAddress("myEmail#gmail.com"),
new MailAddress(receiverEmail));
message.Subject = "Title";
message.Body = $"Text";
// Attach file
Attachment attachment;
attachment = new Attachment("D:\list.txt");
message.Attachments.Add(attachment);
try
{
client.Send(message);
// ALL OK
return true;
}
catch
{
//Have problem
return false;
}
}
and write this on begining of code:
using System.Net;
using System.Net.Mail;
You can choose smpt host adress and port if you want.

Related

How do I send email in C# app using company's email address(webmail). I tried the following codes below but didn't work

How do I send email in C# app using company's email address(webmail)? I tried the following code, but didn't work.
It works with gmail but not with webmail.
using System.Net;
using System.Net.Mail;
private void btnSend_Click_2(object sender, EventArgs e)
{
string from, pass;
MailMessage message = new MailMessage();
from = "london.city#COMPANY.org.uk";
pass = "PASSWORD";
message.To.Add("ABC#gmail.com");
message.Subject = "Hello my friend";
message.From = new MailAddress(from);
message.Body = "I am feeling good";
SmtpClient smtp = new SmtpClient("smtp.webmail.COMPANY.org.uk");
smtp.EnableSsl = true;
smtp.Port = 25;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(from, pass);
try
{
smtp.Send(message);
MessageBox.Show("send successfully");
}
catch (Exception et)
{
MessageBox.Show(et.Message);
}
}

smtp.live Could not send the e-mail - error: Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated

I saw all the other pages on stackoverflow that were about this problem and tried them but none of them worked.
im doing a website as a project for school, and I want the users to send an e-mail for them to report problems in, but it always gives me that error.
this is my code:
protected void Sendbtn_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage = new MailMessage("user#hotmail.com","my#hotmail.com");
mailMessage.Subject = "Problem from Gamer's Utopia";
mailMessage.Body = this.msgtxt.Text;
SmtpClient smtpClient = new SmtpClient(" smtp.live.com");
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
}
I tried using authentication with username and password but it didnt work - unless I did it incorrectly.
add authenticated NetworkCredential
System.Net.NetworkCredential smtpUser = new System.Net.NetworkCredential("admin#hotmail.com", "password");
smtpClient.Credentials = smtpUser;
You need to set SmtpClient Credentials, for example
smtpClient.Credentials = new System.Net.NetworkCredential("youremail#hotmail.com", "password");
check below answer for sample code:
https://stackoverflow.com/a/9851590/2558060
Change your code according to this!!! It will perfectly work!!!
using (MailMessage mail = new MailMessage())
{
SmtpClient client = new SmtpClient("smtp.live.com");
mail.From = new MailAddress("from address");
mail.Subject = "";
string message = "";
mail.Body = message;
try
{
mail.To.Add(new MailAddress("To Address"));
}
catch
{
}
client.Credentials = new System.Net.NetworkCredential("smtp.live.com", "password");
client.EnableSsl = true;
try
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
client.Send(mail);
mail.To.Clear();
}
catch (Exception ex)
{
}
}
From your sender account, view the email inbox and say it is you to allow the third party to send emails.

SMTP Server Error

I've been making a Real Time Modding Tool for Call of Duty and am trying to make a report bug system, but I'm getting this error:
the codes that I'm using for this are as follows:
private void button4_Click(object sender, EventArgs e)
{
// Create the mail message
MailMessage mail = new MailMessage();
// Set The Addresses
mail.From = new MailAddress("brinkerzbhtests#gmail.com");
mail.To.Add("brinkerzbhtests#gmail.com");
// Login to that email
// Set The Content
mail.Subject = "RTM Tool Bug";
mail.Body = textBox1.Text;
// Send The Message
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential info = new NetworkCredential("brinkerzbhtests#gmail.com", "PasswordNotBeingGivenHere");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Picture of full help screen:
NetworkCredential info = new NetworkCredential("brinkerzbhtests#gmail.com", "PasswordNotBeingGivenHere");
smtp.Credentials =info ; // add this line
You create your network credentials and don't associate them with the smtp client.
Try adding the line:
smtp.Credentials = Info;

Sending an email programmatically through an SMTP server

Trying to send an email through an SMTP server, but I'm getting a nondescript error on the smtp.Send(mail); snippet of code.
Server side, the relay IP addresses look correct. Scratching my head about what I'm missing.
MailMessage mail = new MailMessage();
mail.To.Add(txtEmail.Text);
mail.From = new MailAddress("no-reply#company.us");
mail.Subject = "Thank you for your submission...";
mail.Body = "This is where the body text goes";
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mailex.company.us";
smtp.Credentials = new System.Net.NetworkCredential
("AdminName", "************");
smtp.EnableSsl = false;
if (fileuploadResume.HasFile)
{
mail.Attachments.Add(new Attachment(fileuploadResume.PostedFile.InputStream,
fileuploadResume.FileName));
}
smtp.Send(mail);
Try adding smtp.DeliveryMethod = SmtpDeliveryMethod.Network; prior to send.
For reference, here is my standard mail function:
public void sendMail(MailMessage msg)
{
string username = "username"; //email address or domain user for exchange authentication
string password = "password"; //password
SmtpClient mClient = new SmtpClient();
mClient.Host = "mailex.company.us";
mClient.Credentials = new NetworkCredential(username, password);
mClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mClient.Timeout = 100000;
mClient.Send(msg);
}
Typically called something like this:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromAddr");
msg.To.Add(anAddr);
if (File.Exists(fullExportPath))
{
Attachment mailAttachment = new Attachment(fullExportPath); //attach
msg.Attachments.Add(mailAttachment);
msg.Subject = "Subj";
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.ASCII;
msg.Body = "Body";
sendMail(msg);
}
else
{
//handle missing attachments
}
var client = new SmtpClient("smtp.gmail.com", 587);
Credentials = new NetworkCredential("sendermailadress", "senderpassword"),
EnableSsl = true
client.Send("sender mail adress","receiver mail adress", "subject", "body");
MessageBox.Show("mail sent");
This is Adil's answer formatted for C#:
public static class Email
{
private static string _senderEmailAddress = "sendermailadress";
private static string _senderPassword = "senderpassword";
public static void SendEmail(string receiverEmailAddress, string subject, string body)
{
try
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential(_senderEmailAddress, _senderPassword),
EnableSsl = true
};
client.Send(_senderEmailAddress, receiverEmailAddress, subject, body);
}
catch (Exception e)
{
Console.WriteLine("Exception sending email." + Environment.NewLine + e);
}
}
}
You didn't specify the port.
smtp.Port = 1111; // whatever port your SMTP server uses
The SMTP has three different "standard" ports: 25, 465 and 587. According to msdn documentation, the default value for the Port property is 25.

how to send mail by using smtp in asp.net

i need solution for this error
i am run that time some error occur there is:Send Email Failed.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. i1sm8651517pbj.70
using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
#region "Send email"
protected void btnSendmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";
//Default port will be 25
smtpClient.Port = 587;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("muthu17green#gmail.com");
message.Subject = "Feedback";
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1#yoursite.com")
message.CC.Add("muthu17green#gmail.com");
message.CC.Add("muthu17green#gmail.com");
// You can specify Address directly as string
message.Bcc.Add(new MailAddress("muthu17green#gmail.com"));
message.Bcc.Add(new MailAddress("muthu17green#gmail.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtMessage.Text;
// Send SMTP mail
smtpClient.Send(message);
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
}
}
#endregion
#region "Reset"
protected void btnReset_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
#endregion
}
You need to set the SmtpClient.Credentials property:
smtpClient.Credentials = new NetworkCredentials("yourUserName", "yourPassword");
This is what is used to authenticate in order to send the message. You may also need to ensure that SSL is enabled:
smtpClient.EnableSsl = true;
SmtpClient.Credentials Property MSDN Reference
It seems that you are trying to send an email using GMail, which requires SSL.
See this Google reference post.
So in your web.config, enable SSL this way:
<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network host="smtp.gmail.com" port="587" enableSsl="true" userName="YOURUSERNAME" password="YOURPASSWORD" />
</smtp>
</mailSettings>
</system.net>
Alternatively, you can set it programmatically this way:
smtpClient.EnableSsl = true;
I think you forgot to set EnableSSL property to true which is required for gmail.
Here is the sample code:
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtFrom.Text);
msg.To.Add(new MailAddress(txtTo.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtBody.Text;
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential(txtFrom.Text,txtPwd.Text);
mySmtpClient.Host = "smtp.gmail.com";
mySmtpClient.Port=587;
mySmtpClient.EnableSsl = true;
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
mySmtpClient.Send(msg);
msg.Dispose();
lberr.Text="Message sent successfully";
clrtxt();
}
catch(SmtpException)
{
lberr.Text="SMTP Error handled";
}
}
public void SendMail()
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(MailTo.Text);
mail.From = new MailAddress(MailFrom.Text,"Invoice");
mail.Subject = Subject.Text;
mail.Body = Body.Text;
mail.IsBodyHtml = true;
string FileName = Path.GetFileName(FileUploadAttachments.PostedFile.FileName);
Attachment attachment = new Attachment(FileUploadAttachments.PostedFile.InputStream ,FileName);
mail.Attachments.Add(attachment);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("Your_Email#Email.com", "Your_Email_Password");
client.Host = "smtpout.secureserver.net";
client.Port = 80;
try
{
client.Send(mail);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}

Categories