I am trying to send email with attached pdf, my pdf is in byte array. and when i try to send mail (without pdf) it shows
Message = 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
my code is `string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString();
string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(senderEmail, senderPassword);
MailMessage mailMassege = new MailMessage(senderEmail, toEmail, subject, body);
mailMassege.IsBodyHtml = true;
mailMassege.BodyEncoding = Encoding.UTF8;
client.Send(mailMassege);`
and my pdf is in byte array
byte[] applicationPDFData = actionResult.BuildPdf(ControllerContext);
System.IO.File.WriteAllBytes(filePath + "/hello.pdf", applicationPDFData);
I want to to send mail with this pdf.
thanks in advance
You have to create an attachment and append it to the mail Attachemnts list.
here is an example:
byte[] applicationPDFData = actionResult.BuildPdf(ControllerContext);
Attachment attPDF = new Attachment(new MemoryStream(applicationPDFData), name);
EmailMessage emailMessage = new EmailMessage();
emailMessage.To.Add( new EmailRecipient( toEmail ) );
emailMessage.Subject = subject;
emailMessage.Body = body;
emailMessage.Attachments.Add( attPDF );
regrads
gy
Related
I am using SmtpClient to send email with attachment.
Can I delete the sent email from sent folder using SmtpClient ?
Here is my code:
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
using var mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress(s));
mailMessage.From = new MailAddress("mohammad.jouhari#gmail.com");
mailMessage.Subject = "Remote Freelance Web Developer,Mohammad Jouhari Latest CV";
mailMessage.Body = "Dear Hiring Manager,\r\n\r\n Please find attached CV.\r\n\r\n " +
"My work sample:https://github.com/mohammadjouhari.\r\n\r\n" +
"My linkedin Profile: https://www.linkedin.com/in/mohammad-jouhari-42461330/";
string pdfFilePath = "C:\\Users\\m_243\\OneDrive\\Desktop\\microsoft documentation\\.net core\\SendEmailTest" +
"\\SendEmailTest\\wwwroot\\MohammadJouhariCV.pdf";
byte[] bytes = System.IO.File.ReadAllBytes(pdfFilePath);
var attachment = new Attachment(new MemoryStream(bytes), "MohammadJouhariCV.PDF");
mailMessage.Attachments.Add(attachment);
NetworkCredential loginInfo = new NetworkCredential("mohammad.jouhari#gmail.com", ""); // password for connection smtp if you don't have have then pass blank
SmtpClient _smtpClient = new SmtpClient();
_smtpClient.Host = "smtp.gmail.com";
_smtpClient.Port = 587;
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtpClient.EnableSsl = true;
_smtpClient.UseDefaultCredentials = false;
_smtpClient.Credentials = loginInfo;
_smtpClient.Send(mailMessage); // _smtpClient will be disposed by container
_smtpClient.Dispose();
I was trying this code
Pop3Client pop3 = new Pop3Client();
pop3.Host = "smtp.gmail.com";
pop3.Username = "mohammad.jouhari#gmail.com";
pop3.Password = "";
pop3.Port = 587;
pop3.EnableSsl = true;
pop3.Connect();
pop3.DeleteAllMessages();
pop3.Dispose();
I am getting an error in pop3.Connect();
Cannot determine the frame size or a corrupted frame was received
No. The POP3 DELE command deletes from the inbox, not the outbox. Deleting emails from other mailboxes is done through a platform-specific api - the way to do this in Gmail will be different to Outlook. For example, Microsoft uses their Graph api to manage mailboxes. Google calls them 'labels' instead of 'mailboxes' and an email can have more than one label associated with it
Send an email with your domain credential of outlook:
With the following details:
POP Settings
Server: outlook.office365.com
Port: 995
Encryption: SSL/TLS
SMTP Settings
Server: smtp.office365.com
Port: 587
Encryption: STARTTLS
How to create an email service with the exchange in .net core.
MailMessage mailMessage = new MailMessage(_options.Value.Mailbox, receiverEmail, subject, emailBody);
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = UTF8Encoding.UTF8;
mailMessage.CC.Add(_options.Value.Mailbox);
// SmtpClient client = new SmtpClient("smtp.office365.com", 587);
SmtpClient client = new SmtpClient(_options.Value.Server, _options.Value.Port);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
//client.Credentials = new NetworkCredential(senderEmail, senderPassword);
client.Credentials = new NetworkCredential(_options.Value.Email, _options.Value.Password,
_options.Value.Server);
//MailMessage mailMessage = new MailMessage(senderEmail, receiverEmail, subject, emailBody);
await client.SendMailAsync(mailMessage);
return true;
I need to send emails using an exchange server.
I'm trying to set up an automatic email sender.
My smtp server requires authentication. I'm not using ssl but still I'm getting the following response from server:
The server response was: 5.7.1 You are not authorized, authentication is required.
And if I check on the server it got an empty username.
Here is my code:
public void email_send(string mailadress,string docname)
{
string host = "myhost";
string username = "user";
string password = "password";
int port = 625;
var nc = new NetworkCredential(username, password);
var auth = nc.GetCredential(host,port,"Basic");
using (var mail = new MailMessage())
using (var SmtpServer = new SmtpClient())
{
SmtpServer.Host = host;
SmtpServer.Port = port;
SmtpServer.Credentials = auth;
mail.From = new MailAddress("fromadress");
mail.To.Add(mailadress);
mail.Subject = "Subject";
mail.Body = "Body";
Attachment attachment;
attachment = new Attachment("D:/" + docname + ".pdf");
mail.Attachments.Add(attachment);
mail.IsBodyHtml = true;
SmtpServer.DeliveryFormat = SmtpDeliveryFormat.International;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Send(mail);
}
}
When I send mail to my gmail account, it shows below error.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required...
code I am using is below
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
m.From = new MailAddress("me#gmail.com");
m.To.Add("me#gmail.com");
m.Subject = "This is a Test Mail";
m.IsBodyHtml = true;
m.Body = "test gmail";
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("me#gmail.com", "passward");
sc.UseDefaultCredentials = true;
sc.EnableSsl = true;
sc.Send(m);
Response.Write("Email Send successfully");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Just tried your code, had to fiddle with a couple things but was sent this. Funny because I have done this previously using Gmail smtp (couple years back). But it looks like they are now verifying apps that use their platform.
Either use another smtp server that you are signed up to, or use your own. (there must be a test one that is available online??). Pretty sure sendgrid do a free trial.
using System.Net;
using System.Net.Mail;
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "email#yahoo.com";
string password = "abcdefg";
string emailTo = "someone#domain.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);
}
}
Please try this this should work for you
Thank you
I'm developing a simple send mail app in C#, using my CMail Server:
MailMessage mail = new MailMessage("from#mail.com", "destination#mail.com");
mail.Subject = "Sub";
mail.Body = "Hi!";
SmtpClient smtp = new SmtpClient("MyServer");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("user", "pass");
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
smtp.Send(mail);
Obviously i ommited my account information, so, this code throws me an Authentication Exception for some reason. I first thought that the code was wrong, so i change the info to my gmail account and everything goes fine, with the only SMTP server that i having trouble is with the CMail. Is there a problem with .NET and CMail's SMTP ?
Thanks for the help and comments!
Try adding:
smtp.EnableSsl = true;
If you are using 2-step verification then you will need to add application specific password.
Full work sample
public static void sendEmail()
{
//for use GMAIL require enable -
//https://myaccount.google.com/lesssecureapps?rfn=27&rfnc=1&eid=39511352899709300&et=0&asae=2&pli=1
Console.WriteLine("START MAIL SENDER");
//Авторизация на SMTP сервере
SmtpClient Smtp = new SmtpClient("smtp.gmail.com", 587);
Smtp.EnableSsl = true;
Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
Smtp.UseDefaultCredentials = false;
//username password
Smtp.Credentials = new NetworkCredential("rere", "rere");
//Формирование письма
MailMessage Message = new MailMessage();
Message.From = new MailAddress("rere#gmail.com");
Message.To.Add(new MailAddress("rere#gmail.com"));
Message.Subject = "test mesage";
Message.Body = "tttt body";
string file = "D:\\0.txt";
if (file != "")
{
Attachment attach = new Attachment(file, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attach.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
Message.Attachments.Add(attach);
Console.WriteLine("ADD FILE [" + file + "]");
}
try
{
Smtp.Send(Message);
MessageBox.Show("SUCCESS");
}
catch { MessageBox.Show("WRONG"); }
}