I have a windows application and i have a smtp exception which states: "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. z8sm5630522wfj.13
at System.Net.Mail.SendmailAsyncResult.End(IAsyncResult result)
at System.Net.Mail.SmtpTransport.EndSendMail(IAsyncResult result)
at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result)"
Please help in this regard. I need it urgently.
Thanks in advance,
Avinash
Loading
Krishna GaradPosted Feb 28, 2011, 5:55 AM
SmtpClient serverobj = new SmtpClient();
serverobj.Credentials = new NetworkCredential("gmailid","gmailpwd");
serverobj.Port = 25;
serverobj.Host = "Smtp.gmail.com";
serverobj.EnableSsl = true;
Avinash YoganandPosted Feb 28, 2011, 5:50 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;
namespace MailApplication
{
public partial class frMailSendingForm : Form
{
MailMessage msg;
static bool mailSent = false;
public frMailSendingForm()
{
InitializeComponent();
}
private void txtFrom_TextChanged(object sender, EventArgs e)
{
}
private void txtTo_TextChanged(object sender, EventArgs e)
{
}
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient("smtp.gmail.com");
string userState = "test message";
try
{
MailMessage From = new MailMessage("[email protected]", "[email protected]", "Hello", "This is a test message");
MailAddress To = new MailAddress(txtTo.Text.Trim());
msg = new MailMessage();
msg.To.Add(txtTo.Text.Trim());
msg.From = new MailAddress("[email protected]");
msg.Body = rtxtMsg.Text.Trim();
msg.BodyEncoding = System.Text.Encoding.UTF8;
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(msg, userState);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
if (e.Cancelled)
{
string cancelled = string.Format("Send canceled");
MessageBox.Show(cancelled);
}
if (e.Error != null)
{
string error = string.Format(e.Error.ToString());
MessageBox.Show(error);
}
else
{
MessageBox.Show("Message Sent Successfully");
}
mailSent = true;
msg.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
string token = (string)e.UserState;
if (e.Cancelled)
{
MessageBox.Show("[{0}] Send canceled", token);
}
if (e.Error != null)
{
}
else
{
MessageBox.Show("Message sent");
}
mailSent = true;
}
private void txtSubject_TextChanged(object sender, EventArgs e)
{
txtFrom.Text = txtSubject.Text.Trim();
}
static void AttachmentFromFile()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "This is a test mail";
mail.Body = "this is a test mail";
mail.Attachments.Add(new Attachment("C:\\Users\\Avinash\\Documents\\Desktop\\gopi sir mail id.txt"));
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
smtp.Send(mail);
}
}
}
Please help me with this
Krishna GaradPosted Feb 28, 2011, 5:25 AM
Avinash YoganandPosted Feb 28, 2011, 4:59 AM
Please help me with the code. Also i wanted code for attaching files and sending mail via the same windows application.
Krishna GaradPosted Feb 28, 2011, 3:47 AM