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("[email protected]");
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("[email protected]", ""); // 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 = "[email protected]";
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
CodePudding user response:
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