Emailing an attachment in C# Windows application
We are developing a .Net Windows application in C# for commercial distribution.
Objective
I want to add a function to allow the user to email a file to any email recipient from within the application without needing an email client on their PC, just an internet connection. The email they send needs to appear that it has come from them (i.e. their email address is in the FROM/REPLYTO field) so that the recipient can identify where the email has come from and they can reply to it.
Current Approach
I was intending to use the MailMessage class in System.Net.Mail, but it requires an SMTP server to send the email. The problem with that is, i dont want the user to have to configure anything to send emails from our application - i want it set up by us. In other words, i want to define what SMTP server to send the email from and hardcode it in the application.
I have tried to use Gmail as the SMTP server, however one major drawback - Gmail automatically rewrites the "From" line of any e-mail you send via their SMTP gateway to your Gmail address, so when we tested 开发者_JAVA百科this, even though in the MailMessage code we set the FROM property to myemail@mycompany.com, when the email was received, the From field displayed mygmailemail@gmail.com
Questions
If this software application is going to be distributed to many users in different network environments, is this method the best to use? If so, is there a free/public SMTP server i can use for our application to achieve this that will allow the end-user to define the FROM and REPLYTO fields?
I have been told that not all ISPs / organisations will permit arbitrary SMTP connections and/or if the sender implements an SPF record the message may be rejected. Does that mean my intended approach may not always work?
Is there any other common or more stable method i could use to achieve my original objective?
I don't think you can guarantee any smtp server will always be available. e.g. gmail's smtp is blocked at my office.
You're probably better off creating a web service that they send the attachment to and then you route it through your mail server.
1) I'd be surprised if you found a public SMTP server to do this as it would just be used heavily as a spam relay.
2) A lot of residential ISPs do block SMTP traffic from leaving their network. However, they are doing this by blocking port 25 so if you host your own SMTP server on an alternate port it won't get blocked except in the most extreme cases.
3) see below
I see two items you have to address:
Getting the message from the sender to you
Conrad's suggestion of using a web service is a good one as it gives you more control over the transport of the message to you. You can implement whatever authentication you deem necessary to make sure the person sending the message is legit. You could use SMTP to send the messages through a mail server hosted by you, but would need to focus on knowing who to safely allow to send and who not to so you don't end up as a spam relay.
Sending the message from you to the recipient
You can still send the message to the final recipient via SMTP to their mail host. I would suggest sending the email with the from as your application (ex: mailagent@myapplication.com) and define the reply-to address for your original sender. If you try using their address in the from, you will get more spam rejections.
If I understood your description, the messages will only be being sent to other users of the application?
If so, you could take the web service route on both parts of the communication. Sender delivers the message to your centralized server over a web service call, when the recipient user opens the software, it checks with your server to see if there are any messages waiting for them.
EDIT:
Yes, the part on the destination emails, I could read it both ways, guess I leaned toward the wrong one.
You will have to deliver the messages in the end via smtp, I still suggest using your own from and put the sender in the reply-to unless these are corporate customers that you can get to add your smtp server to their SPF records.
Also, investigate specific major mail targets like Yahoo! and GMail for how to interact with them such as:
http://mail.google.com/support/bin/answer.py?hl=en&answer=81126
http://help.yahoo.com/l/us/yahoo/mail/postmaster/postmaster-15.html
http://help.yahoo.com/l/us/yahoo/mail/postmaster/bulkv2.html
I've used something similar to this to send email without having to rely on smtp. YMMV
精彩评论