Receiving email and downloading attachment through a C# Application - c#

I am trying to implement an application which can receive the mails sent to a specific email address. The scenario is that the user will send a .ppt file as an attachment to a specific email address, and my WPF application will listen to this email and once it receives the email, it will download the attached file - saving it to the hard drive.
I looked a bit, but all I found was the System.Net.Mail objects which only support sending emails through an application using the System.Net.Mail.SmtpClient class.
How can I do this in C#?

var client = new POPClient();
client.Connect("pop.gmail.com", 995, true);
client.Authenticate("admin#bendytree.com", "YourPasswordHere");
var count = client.GetMessageCount();
Message message = client.GetMessage(count);
Console.WriteLine(message.Headers.Subject);
A simple tip, that you can follow:
https://joshwright.com/tips/tips-sending-receiving-email-in-csharp/

You can use POP3 or IMAP to check for email messages and then process the email message for saving the attached .ppt file. Click here for a sample.

Related

POP3 not fetch newest emails on GMail

I use mailkit pop3 to fetch emails, but I find it didn't fetch newest emails and each time I run it return different messages count number
using (Pop3Client client = new Pop3Client())
{
// Connect to the server
client.Connect(hostname, port, useSsl);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate((username), password);
int messageCount = client.Count;
// var folder = client.;
// We want to download all messages
List<MimeMessage> allMessages = new List<MimeMessage>(messageCount);
for (int i = messageCount - 1; i > 0; i--)
{
var msg = client.GetMessage(i);
allMessages.Add(msg);
}
}
I supposed that all messages variable should be filled with all emails ordered from newest emails to old emails but I found all messages return old emails and not contains at all new emails at all, I searched a lot and didn't find the reason, Do you know why that happened and how can I fix that ?
I wish to find any help here and Thanks in advance
Are you connecting to GMail, by any chance?
This is a GMail behavior where, depending on your POP settings (as set in the GMail web app Settings page), will determine what messages the client sees.
This is not a MailKit issue.
From Google's own FAQ in the section titled "Emails aren't downloading correctly", it states:
After you set up POP in your Gmail settings, your emails become available
in batches. It might take a while to see all your emails.
Note: Gmail downloads a copy of every email you send or receive, except
for emails in Chats, Spam, and Trash. To avoid duplicates, Gmail doesn't
download emails sent within your mail client, but you can still see them
if you log in to Gmail.
If you continue to have problems downloading emails, try using recent
mode:
In your email client's POP settings page, find the "Email address" or
"User name" field.
Add recent: in front of your email address. For example,
recent:example#gmail.com.
If that doesn't fix the problem, try deleting your Gmail address from your email client, then re-adding it.

Email not reaching a specific email domain from php or C#

I am currently using an Office 365 account to send email from various websites. I have implemented an smtp client in both php and C# to send the mail. I use GoDaddy and Rackspace to hosts my sites.
The mail reaches most address without issue, however, I have a business domain through Google, which ends with .co, and emails do not make it to my inbox there.
Here are the settings for the PHPMailer client:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = "tls";
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = '<myuser>';
$mail->Password = '<mypassword>';
$mail->Port = 587;
$mail->setFrom('noreply#<mymaildomain>.com', 'MyMail');
The settings send email find to yahoo, gmail, etc. However, when I try to send it to my .co email address, the client indicates the mail was sent, but it never shows up in the inbox.
I am wondering if it is an issue with a domain in Google or if the .co is the problem. Is there any special setting I need to make on the client for .co addresses?
UPDATE
I changed phpmailer to simply use php's mail function. The mail was sent through to my .co domain, but I still have no idea why it will not go through when an authenticated SMTP user sends it. I can go into my Office 365 account and send email to the .co address without issue. I just cannot push it from the server. My host server does not contain the smtp server.

Faxing from Faxmail.com not working with more than 250KB file

We have used Faxmail.com to send fax (Email to fax) from one of our ASP.NET application, and it has been working from ages, suddenly its failing to send fax if the file size is more than 250 KB. We have been sending fax with file size up to 8 MB with same application before.
Eg :-18180000000#faxmail.com
Send the email message from SMTP to the desired fax user and it used to deliver to fax machine.
SmtpClient smtp = new SmtpClient();
MailMessage nm = new MailMessage();
smtp.Host = "smtp.domain.com";
smtp.Send(nm);
We don't see any exceptions while sending email, however its not reaching fax machine (receipient).
Note :- This was been working since early 2000.
You might want to start by checking your mail server logs. These should show whether or not the message was delivered to faxmail's mail server - and if so, you should see a receipt acknowledgement from faxmail's mail server. If the record shows successful delivery, then you can forward the record to faxmail, and they should be able to troubleshoot to find the reason that the message wasn't processed by their system. If the mail server logs show an error delivering the message, there should be a description that you can go on to troubleshoot. If you don't find a record at all, then it means the problem is on your end - the message is getting dropped somewhere before it is sent.
We found the issue was at the easylink server which is the service provider for faxmail.com. Easylink was failing to render the PDF attachment that was being sent. Upgrading the server with latest PDF patch resolved the issue.

change sender address when sending mail through gmail in c#

I have used the following code to send mail from my web application using a gmail account. My question is, can i change the sender address to another address other than original sender(gmail) address?
My code is as follows:
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("sample#gmail.com", "*******");
Whatever i do is useless as i always receive mail from sample#gmail.com. Is it possible to change it?
I have changed to
mail.From = new System.Net.Mail.MailAddress("sample#yahoo.com"); but i received the mail with the from address sample#gmail.com and not from the new "From" address. I think gmail smtp overwrites the from address with the original credential.
This is the solution:
use the codes above to set mail.From = new MailAddress(address, display name)
in Gmail, go to Mail Settings >> Accounts and Import.
Add the email account you will use as sender in "Send Mail As". (tick as Alias)
This works for me
Gmail doesn't allow you to change the FROM to something different than your gmail account.
It doesn't matter what you use, they over-write it, before they relay it on. This prevent spamming/spoofing.
Yes just use the From property of the MailMessage
eg.
mail.From = "newemail#email.com";
EDIT: Also, see this post for more detailed info on how to emails via gmail in C#
Sending email in .NET through Gmail
EDIT: Although this works for mail in general, it appears this won't work for gmail as google overwrite it before its sent (see #Dave wanta's answer)
If you have a limited number of senders you can do as #philip suggested. For instance you may have customerservice#example.com, simon#example.com and philip#example.com or even alias#example.com. As long as they are approved senders on the actual gmail.com website you can send from them.
Gmail.com : Sending mail from a different address
If you are expecting to send from an arbitrary user (such as a customer service form on a website where the user enters their email and you don't want them emailing you directly) about the best you can do is this :
msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));
If you're in a controlled environment this works great, but please note that I've seen some email clients send to the from address even when reply-to is specified (I don't know which).
Check #56 and #58. They might be relevant to what you want to do
https://code.google.com/p/google-apps-script-issues/issues/detail?id=172

How can I get the unread/new messages from Gmail using POP3?

Using the OpenPOP .net client for getting messages from Gmail.
I'm wondering how I can get only the new messages?
Currently, I get the atom feed and then get as many emails as the feed has with the OpenPOP client (starting from the first).
GmailAtomFeed feed = new GmailAtomFeed("user", "pass");
feed.GetFeed();
int unread = feed.FeedEntries.Count;
POPClient client = new POPClient("pop.gmail.com", 995, "user", "pass", AuthenticationMethod.USERPASS, true);
for (int i = 0; i < unread; i++)
{
Message m = client.GetMessage(i, false);
// ...
}
Is there a better way to do this?
And how do I set the unread messages to be read?
I doubt you can do it with pop3. From my understanding POP3 doesn't support the notion of the unread\unseen email. It should be up to the client to track messages which were already shown to the user and which were not.
What you can do is switch to using IMAP protocol to access gmail. Check this link for how you can switch it on for your gmail account Getting started with IMAP for Gmail.
Now, if you're using c# there are some commercial libraries for IMAP and there free\opensource ones: like this one on codeproject: IMAP Client library using C#. What you have to do to get unseen messages is to specify "unseen" flag for the select command. Here's an example
You have to store the UIDL of each email in a local database. When you want to check for new mail, you retrieve the UIDLs on the server and see if you have if already in your local database; if not, it's a new mail.
Outlook uses the same strategy.
same Q How to retrieve only new emails using POP3 protocol

Categories

Resources