Using Mail.dll with Microsoft Exchange Imap version 4 - c#

I am using mail.dll for reading emails. Earlier I was reading the emails from a gmail account and every thing was working alright. Now I need to move the application to another server and read mail from microsoft exchange.Below is the code that I am using
using (Imap imap = new Imap())
{
imap.Connect("server-name", port number);
imap.Login("username", "password");
imap.SelectInbox();
List<long> uids = imap.Search(Flag.Seen);
if (uids.Count > 0)
{
string eml = imap.GetMessageByUID(uids[0]);
IMail email = new MailBuilder().CreateFromEml(eml);
imap.GetMessageByUID(uids[0]);
lbResponse.Text = email.Subject;
}
else
{
lbResponse.Text = "No mails found";
}
imap.Close();
}
I have verified the port and server by using telnet and it is correct. But I get the "Unable to read data from the transport connection. A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond " error. Please help.

Make sure that IMAP is enabled on your Exchange server:
http://technet.microsoft.com/en-us/library/bb124489.aspx
Also consider using:
imap.Search(Flag.Seen)
SearchFlag is obsolete in the latest version: http://www.limilabs.com/mail

Related

Sending mail using gmail and .net

Getting the exception:
Failure sending mail.
While using System.Net.Mail.Smtp
in C#.NET on the line smtp.Send(message);
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(sendto);
message.Subject = "CP1 Lab Password";
message.From = new System.Net.Mail.MailAddress("abc#gmail.com");
message.Body = mail_message;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Host = "smtp.gmail.com";
smtp.Port = 465; //(465 for SSL)587
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "mypassword");
smtp.Send(message);
Edit 1:
Here is the detail of the error:
A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond ...
The same error when using port 25.
Few months before this code used to work but today it is not working
As from your exception message (incomplete) and the code, it is very hard to say what went wrong in your case. Failure sending mail is thrown when the server is not found, port cannot be connected. It means that connection was not established, until now.
The exception message that gets raised if the connection was established but authentication was not performed correctly is, "The SMTP server requires an authenticated connection...". I would suggest that you check the PORT, SMTP server host (do not add http://) and then re-try. An SMTP connection (in most general cases) requires
SMTP Server host: smtp.gmail.com is enough!
PORT to connect at: I have always used default TCP port for SMTP, 25. It works.
EnableSsl: most require it. I suggest you always use it. client.EnableSsl = true;
Credentials are required by all: No server would allow bots sending emails. In this concern, if you create a new account. You may face trouble sending emails programmatically, I faced the problem with a new account not sending the emails whereas an old account (default account of mine) was sending the email without any trouble.
The following code template (if filled with accurate parameters) will definitely send the email, because I have tested and verified it bazillion times. :)
// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
// Configure the client
client.EnableSsl = true;
client.Credentials = new NetworkCredential("<username>", "<password>");
// client.UseDefaultCredentials = true;
// A client has been created, now you need to create a MailMessage object
MailMessage message = new MailMessage(
"from#example.com", // From field
"to#example.com", // Recipient field
"Hello", // Subject of the email message
"World!" // Email message body
);
// Send the message
client.Send(message);
/*
* Since I was using Console app, that is why I am able to use the Console
* object, your framework would have different ones.
* There is actually no need for these following lines, you can ignore them
* if you want to. SMTP protocol would still send the email of yours. */
// Print a notification message
Console.WriteLine("Email has been sent.");
// Just for the sake of pausing the application
Console.Read();
}
Sending an email may be a headache sometimes, because it requires some basic understanding of networking also. I have written an article that covers sending emails in .NET framework and a few problems that beginners usually stumble upon such as this one. You may be interested in reading that article also.
http://www.codeproject.com/Articles/873250/Sending-emails-over-NET-framework-and-general-prob

Email to yahoo from C# application error

I am able to ping smtp.mail.yahoo.com from my system but when i send email from following code using yahoo address it gives error transport failed to connect to server.
The same code successfully sends the email from gmail account.
I am using port 465 for yahoo.
MailMessage oMsg = new MailMessage();
oMsg.From = from.Text;
oMsg.To = to.Text;
oMsg.Subject = "Hi";
oMsg.BodyFormat = MailFormat.Html;
oMsg.Body = msg.Text;
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", host);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
WebProxy proxy = WebProxy.GetDefaultProxy();
if (proxy.Address != null)
{
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/urlproxyserver", proxy.Address.Host);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/proxyserverport", proxy.Address.Port);
}
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",from.Text);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pass.Text);
// ADD AN ATTACHMENT.
/* MailAttachment oAttch = new MailAttachment(path+ "\\Image.bmp", MailEncoding.Base64);
oMsg.Attachments.Add(oAttch);*/
SmtpMail.SmtpServer.Insert(0,host);
if (proxy.Address != null)
MessageBox.Show("Sending via proxy settings: " + proxy.Address.ToString());
try
{
SmtpMail.Send(oMsg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
oMsg = null;
Any ideas why this error occurs?
Being able (or not) to ping a host does not say anything about whether you will be able to connect to a particular service on it. For that, you need to try to actually establish a connection. (And of course, the fact that you can establish a connection does not necessarily imply that the service in question is working properly.)
Usually, it's a good idea to use telnet to try connecting to the remote host on the port in question. The syntax on the command line is simply telnet host.fqdn.example.com portnumber. This will tell you if there is anything at all at the other end of the pipe responding to connection attempts, which is a first step in determining where the problem is.
Second, it's usually a good idea to trim the code to the minimal version that exhibits the problematic behavior, and include the full code to show the problematic behavior. You are using a number of variables in your code which we really know nothing about.
Some ISPs block outgoing connections to the SMTP ports on hosts other than their own mail servers, to reduce the amount of outgoing spam. Maybe there is a typo in the value in host? Maybe you are inadvertantly using some unexpected MailMessage implementation? And so on.
That said, I would definitely first try to connect to the mail server in question manually, through a proxy if you are using one to connect using that code. If that doesn't work either, then your problem at least has nothing to do with the code in the question, and you can look elsewhere (in which case one possible candidate would be ISP filters; maybe they have a list of allowed external SMTP hosts and Yahoo's isn't on it?).

Why do I receive Invalid Password for some Hotmail Accounts while trying to connect through POP3?

I am making a software to check a list of hotmail accounts for something. I am using OpenPop.NET Library. The weird thing is that I get Invalid Password Exception with some accounts while I am sure that the password is correct because I can login without pop3 normally. I'd appreciate if someone can provide me an answer or an alternative way to connect to Hotmail inbox other than pop3 (Don't suggest using a Web Browser).
Here is the code I am using to connect:
Pop3Client client = new Pop3Client();
client.Connect("pop3.live.com", 995, true);
var username = file[i].Split(':')[0];
var pass = file[i].Split(':')[1];
try
{
client.Authenticate(username, pass); //I am pretty sure that username,pass holds the right values.
var msgs = client.GetMessageCount().ToString();
updateList(i, msgs); //updates a listbox with message count for the hotmail account
}
catch (OpenPop.Pop3.Exceptions.InvalidPasswordException)
{
updateList(i, "Invalid Password");
}
Thanks.
Edit: Here is the server response that I get from non-working accounts
The server didn't respond with +OK response. The response was: "-ERR mailbox could not be opened"
It sounds like the server is trying to prevent brutal force attacks !!! as you are checking credentials for a list of accounts. You may want to check the error message or inner-exception

how to read email from gmail using c#

I want to create window application through which i can read email from gmail.
Actually i want to read proper format of email like to,from,subject,cc and body.
using (Imap imap = new Imap())
{
imap.ConnectSSL("mail.company.com");
imap.Login("angel_y#company.com", "xyx***");
imap.SelectInbox();
List<long> uids = imap.SearchFlag(Flag.Unseen);
foreach (long uid in uids)
{
string eml = imap.GetMessageByUID(uid);
IMail message = new MailBuilder()
.CreateFromEml(eml);
Console.WriteLine(message.Subject);
Console.WriteLine(message.TextDataString);
}
imap.Close(true);
}
It is this error.
No connection could be made because the target machine actively refused it
Try this I have added the Port number along with the gmail imap server for connection to the server
using (Imap imap = new Imap())
{
imap.ConnectSSL("imap.gmail.com", 993);
imap.Login("angel_y#company.com", "xyx***"); // MailID As Username and Password
imap.SelectInbox();
List<long> uids = imap.SearchFlag(Flag.Unseen);
foreach (long uid in uids)
{
string eml = imap.GetMessageByUID(uid);
IMail message = new MailBuilder()
.CreateFromEml(eml);
Console.WriteLine(message.Subject);
Console.WriteLine(message.TextDataString);
}
imap.Close(true);
}
I am sure there are many libraries to do this. A quick search turned this up:
http://code.msdn.microsoft.com/CSharpGmail
And here is a gadget / widget app that has some code to do this:
http://www.codeproject.com/KB/gadgets/GadgetInterop.aspx
You may need to make sure you are using the correct hostname and port number. Configuring these settings will depend on the IMAP API you are using for .Net
But the settings you want to use are listed on google's site.
IMAP => imap.google.com:993 (SSL)
SMTP => smtp.google.com:587 (TLS)
gmail offers access via its config page to pull down emails through POP3/IMAP. Here are a few such links I found off of Google that could be used for IMAP access.
http://www.codeproject.com/KB/IP/imaplibrary.aspx
Accessing Imap in C#
http://koolwired.com/solutions/solutions.aspx?id=30
Hopefully that helps!

send email C# using smtp server with username password authentification

I have a piece of code that sends email.. heres the code
This is not working for me. This a remote smtp service ... and i double checked that email web access works fine .. i can login using the gui, recieve and send emails.
But when i try to do it through code .. it fails with the message ...
{System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 No AUTH command has been given.
Can anybody advise ... and also they dont have EWS exposed ie.e exchange web service ./.. this is the way to go ..
port is 25 and no SSL or TLS
Button b = sender as Button;
try
{
MailMessage msg = new MailMessage(senderEmail, recieverEmail, "afdasfas", "safasfa");
//MailMessage msg = new MailMessage(senderEmail, recieverEmail, subject, subject);
System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient(EmailSmtpServer, outgoingPort);
System.Net.NetworkCredential auth = new System.Net.NetworkCredential(senderEmail, senderPassword);
mailclient.Host = EmailSmtpServer;
mailclient.UseDefaultCredentials = false;
mailclient.Credentials = auth;
mailclient.Send(msg);
MessageBox.Show(b.Content + ":WORKED");
}
catch (Exception e4)
{
MessageBox.Show(b.Content + ": " +e4.Message);
MessageBox.Show(b.Content + ": " + e4.StackTrace);
}
I don't think you want to set
mailclient.UseDefaultCredentials = true;
Read more about this option here.
You should also test that you can actually send email through that smtp server.
Try connecting via telnet and authorizing. There are also plenty of services that will let you send a test message online (http://pingability.com/smtptest.jsp for example).
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 No AUTH command has been given.
you are attempting to login without sending the AUTH command first. this is why your web gui, which auto-inserts it's own AUTH commands works but your current client-application is not receiving a command from you to relay onto the server, but the server requires authentication.
maybe you need to Add mailclient.EnableSsl = true;

Categories

Resources