开发者

Sending Emails from a WPF Application

开发者 https://www.devze.com 2023-02-25 08:57 出处:网络
I am building a WPF application, and 1 of the requirements is for the user to send me feedback through an email.

I am building a WPF application, and 1 of the requirements is for the user to send me feedback through an email.

Now I have developed a method found on the Microsoft Site :-

http://support.microsoft.com/kb/310263

which worked. However I have some reservations about it since what if the user does not have Outlook installed? And also I do not like the popup command where it asks you if its ok to use Outlook to send the mail.

开发者_开发技巧Is there any other solution? I do not wish to ask the user for his SMTP Host since most of my users won't know how to get that.

Thanks for your help and time


I've also had this problem and decided to bypass it by sending e-mail from my server.

The problem is that to send e-mail you need the user's server info (SMTP host, username, password, e-mail address) or, to use software already on the system that knows this info already (outlook) - and that assumes the user can send SMTP mail at all (some webmail services don't have an SMTP option).

So what I've done is to post the content from my app to a simple ASP.net application on my web site and send the mail from there - that way I don't need to worry about SMTP and it also works great with my spam filtering (the app only sends e-mail to me so it can't be used by spammers and it bypasses my own spam filtering so I don't have to worry about losing feedback messages).

Update - here is the example you requested:

Here is my helper method for doing simple HTTP POST:

public static string HttpPost(string URI, string Parameters)
{
    System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";
    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
    req.ContentLength = bytes.Length;
    System.IO.Stream os = req.GetRequestStream();
    os.Write(bytes, 0, bytes.Length);
    os.Close();
    System.Net.WebResponse resp = req.GetResponse();
    if (resp == null) return null;
    System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
    return sr.ReadToEnd().Trim();
}

and the usage is:

            HttpPost("http://example.com/Default.aspx",
                "&email=" + Uri.EscapeDataString(form.EMail) +
                "&data=" + Uri.EscapeDataString(errorText));

In this example errorText is the multi-line return value from Exception.ToString()

On the server side there's a simple IHttpHandler (but it can be any server side app, originally it was a WebForms page) that reads the data from HttpContext.Request.Params, formats it as an e-mail and sends it to me using my own mail server - but you can just put it in a database if you want to bypass the whole e-mail thing (I wrote it using e-mail because it saved me writing the backend admin and let me reuse my contact form app)

0

精彩评论

暂无评论...
验证码 换一张
取 消