I am using OpenPop.NET and I am trying to download the emails from the server. But, it is giving the below error message In the meantime, I could configure and download the mails without any issues in Outlook by using the same settings that I did with OpenPop.NET.
The server did not respond with a + response. The response was: "-ERR Command is not valid in this state."
FetchAllMessages(smtpserver, 110, false, emailId, password);
public static List<Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password)
{
using (Pop3Client client = new Pop3Client())
{
client.Connect(hostname, port, useSsl);
client.Authenticate(username, password);
int messageCount = client.GetMessageCount();
List<Message> allMessages = new List<Message>(messageCount);
for (int i = messageCount; i > 0; i--)
{
allMessages.Add(client.GetMessage(i));
}
return allMessages;
}
}
throwing exception in client.Authenticate(username, password);
Related
I used S22.imap package to read all unread emails like below, then I want to mark the unseen mail as seen after reading it, how can I do this please.
string host = "host";
int port = port;
string username = "username";
string password = "psw";
using (ImapClient client = new ImapClient(host, port, username, password, AuthMethod.Login, true))
{
IEnumerable<uint> uids = client.Search(SearchCondition.Unseen());
// Download mail messages from the default mailbox.
IEnumerable<MailMessage> messages = client.GetMessages(uids, FetchOptions.Normal);
foreach (var item in messages)
{
string from = item.From.ToString();
string body = item.Body.ToString();
string subject = item.Subject.ToString();
Console.WriteLine(from + "-" + body + "-" + subject);
}
}
Try:
MessageFlag[] flags = new[] { MessageFlag.Seen };
client.SetMessageFlags(id, null, flags);
I'm trying to send an automated email from my desktop application using Visual Studio C# and SMTP. It works when directly connected to the Wi-Fi but the moment I connect to the VLAN that we set up, the SmtpClient.Send() times out.
public static void SendEmail(string toAddress, string subject, string body)
{
string senderID = "email";
const string senderPassword = "password";
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
if (IsValidEmail(toAddress))
{
MailMessage message = new MailMessage(senderID, toAddress, subject, body);
smtp.Send(message);
}
else throw new Exception();
}
catch (Exception)
{
throw;
}
}
We're using static IP, so I thought maybe it had something to do with DNS since the SMTP host isn't a specific IP.
I have this code:
public List<Attachment> GetAttachments() {
string hostname = "pop.gmail.com";
int port = 995;
bool useSSL = true;
string attachmentType = "application/pdf";
string email = "myemail#gmail.com";
string emailFrom = "someone#gmail.com";
string password = TxtBoxPassword.Text;
if (!string.IsNullOrWhiteSpace(password))
{
Pop3Client client = new Pop3Client();
client.Connect(hostname, port, useSSL);
client.Authenticate(email, password, AuthenticationMethod.UsernameAndPassword);
List<Attachment> listAttachments = new List<Attachment>();
int count = client.GetMessageCount();
for (int i = count; i >= 1; i--)
{
Message message = client.GetMessage(i);
if (message.Headers.From.MailAddress.Address == emailFrom)
{
List<MessagePart> attachments = message.FindAllAttachments();
foreach (MessagePart attachment in attachments)
{
if (attachment.ContentType.MediaType == attachmentType)
listAttachments.Add(new Attachment(attachment));
}
}
}
}
}
To read all emails in email account.
It access the email account and get 265 emails from sent/inbox folders.
Currently I have over thousand emails in the account, so I expect to see this number on count of emails.
What is missing in code/Gmail account settings that is preventing me to get all emails?
Thanks
Well, gmail has some quirks when it gets to it's POP3 features. See my answer on What non-standard behaviour features does Gmail exhibit, when it is programmatically used as a POP3 server?. I do not think you can alter any settings that will remedy your problem.
I am trying to connect to server via TLS 1 on Windows.
here is how I am connecting to server
public void Connect(string hostName, int port)
{
this.tcpClient.Client.Connect(hostName, port);
RemoteCertificateValidationCallback validationCallback = new RemoteCertificateValidationCallback(ServerValidationCallback);
LocalCertificateSelectionCallback selectionCallback = new LocalCertificateSelectionCallback(ClientCertificateSelectionCallback);
EncryptionPolicy encryptionPolicy = EncryptionPolicy.RequireEncryption;
this.sslStream = new SslStream(this.tcpClient.GetStream(), true, validationCallback, selectionCallback, encryptionPolicy);
//handshake
X509CertificateCollection clientCertificates = GetCertificates();
this.sslStream.AuthenticateAsClient(hostName);
//this.sslStream.AuthenticateAsClient(hostName, clientCertificates, SslProtocols.Tls, true);
}
Than I am sending messages via method
this.sslStream.Write(messageBytes);
this.sslStream.Flush();
this.tcpClient.GetStream().Flush();
Server did not receiving anything from me.
Ive written an application which sends notifications to people via email. For testing purposes im using Gmails SMTP server. Using the below code im testing but im randomly getting the below error.
I'm using smtp.gmail.com on port 587
System.Net.Mail.SmtpException: 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
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EmailAlertService.Email.sendMail(String subject, String body, String[] recipients, String from, String& error)
About 1 out of 5 emails generates this exception.
I'm using the same credentials and to/from address combo for every test.
Any idea if its something im doing or is it Gmails servers throwing a fit!
public bool sendMail(string subject, string body, string[] recipients, string from, ref string error)
{
bool success = recipients != null && recipients.Length > 0;
if (success)
{
SmtpClient smtpClient = new SmtpClient
{
Host = hostName,
Port = port,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(username, password)
};
using (MailMessage gMessage = new MailMessage(from, recipients[0], subject, body))
{
Console.WriteLine(recipients[0]);
for (int i = 1; i < recipients.Length; i++)
{
Console.WriteLine(recipients[i]);
gMessage.To.Add(recipients[i]);
}
try
{
smtpClient.Send(gMessage);
success = true;
error = string.Empty;
}
catch (Exception ex)
{
success = false;
error = ex.ToString();
}
}
}
else
error = "No destination email addresses supplied";
return success;
}