Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Sending emails problem()

$
0
0

Hi, Iam preparing a small interview email project in VS2015-Asp.net with c#. But the same project works well in Windows Forms.
But when I try to send from WebForms its' giving error like

" The SMTP server requires a secure connection or the client has not authenticated. The server response was 5.5.1 authentication required"

My Codes :-

try
{
    System.Net.Mail.MailMessage Mail = new System.Net.Mail.MailMessage();
    System.Net.Mail.SmtpClient SmtpServer = new System.Net.Mail.SmtpClient("smtp.gmail.com");
    Mail.From = new System.Net.Mail.MailAddress("paramu@gmail.com");
    Mail.To.Add(new System.Net.Mail.MailAddress(this.DropDownList1.SelectedItem.Text));
    if (TextBox1.Text.Trim().Length > 0)
    {
        Mail.Bcc.Add(this.TextBox1.Text);
    }
    if (TextBox2.Text.Trim().Length > 0)
    {
        Mail.CC.Add(this.TextBox2.Text);
    }
    Mail.Subject = this.TextBox3.Text;
    if (MyInput1.PostedFile.FileName.Trim().Length>0)
    {
        System.Net.Mail.Attachment My_Attach = new System.Net.Mail.Attachment(MyInput1.PostedFile.FileName);
        Mail.Attachments.Add(My_Attach);
    }
string MyImgString64 = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBD....";
Byte[] bitmapData = Convert.FromBase64String(FixBase64ForImage(MyImgString64));
System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData); 
System.Net.Mail.LinkedResource imageToInline = new System.Net.Mail.LinkedResource(streamBitmap, System.Net.Mime.MediaTypeNames.Image.Jpeg);
imageToInline.ContentId = "MyImage";
System.Net.Mail.AlternateView av1 = System.Net.Mail.AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:MyImage/><br></body></html>" + TextBox4.Text, null, System.Net.Mime.MediaTypeNames.Text.Html);
av1.LinkedResources.Add(imageToInline);
Mail.AlternateViews.Add(av1);
Mail.IsBodyHtml = true; 
Mail.BodyEncoding = System.Text.Encoding.UTF8;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("paramu@gmail.com", "123456");
SmtpServer.EnableSsl = true;
SmtpServer.Send(Mail);
MessageBox.Show(this, "Sending Mail Status -> Success....!!!");
}
catch (Exception ex) {
   MessageBox.Show(this, ex.Message);
}

Thanks Again


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>