wpf, send mail, let the user select the from field - c#

I'm trying to launch the email app from the user click some hyper link, I want to add also attachment, the only thing that worked was this link
enter link description here, but now I don't want to specify the MailMessage.From field, I want it to be like when you click on mailto: link, it opens your deafault account or ask you to login, etc.., but whenever I try to set the mailMessage.From = new MailAddress("");, it throws exception, it should be an email
to get my app launch the mail with attachment, now how can I remove the field "From" and let the user use his mail or something like when you use mailto:
if I remove this line, it throws exception
mailMessage.From = new MailAddress();

If you want your WPF application to send an e-mail directly you should use the SmtpClient Class MSDN.
If you are trying to launch the default mail app (like outlook) to send the e-mail, I'd use Process.Start as one of the answers to the question linked specified.
var url = "mailto:user#domain.com";
// var url = "mailto:?subject=aSubject&body=aBody";
// var url = "mailto:?subject=aSubject&body=aBody&attachment=aFile";
Process.Start(url);

Related

Given Email display name not updated in outlook

I want to change my email display name which is sent by my MVC application.
actually, the email address is: sample#company.com
the default display name is: Company Sample.
Now i want to change that display name into "SomeOne" but it not works. i have tried below items,
Tried Email display name property:
MailAddress from = new MailAddress("sample#company.com", "SomeOne");
It works fine in Gmail but in outlook, the display name not changed.
MailAddress from = new MailAddress("sample#company.com", "\\SomeOne\\");
It will change the display name in outlook but double quote(") added at the last like this
SomeOne"
objMail.From = new MailAddress("<DisplayName>EmailAddress#domain.com");
Not works.
Can you please provide any suggestions..?
Thanks,
Nagaraj M
Your first address is correct
MailAddress from = new MailAddress("sample#company.com", "SomeOne");
And I believe the third is backwards
//objMail.From = new MailAddress("<DisplayName>EmailAddress#domain.com");
objMail.From = new MailAddress("DisplayName<EmailAddress#domain.com>");
Outlook presents other challenges. If the address is in your Outlook contacts it may overwrite the friendly address coming in. Same if you are sending from Outlook to a previous friendly address. In most cases clearing out the Most Recently Used (MRU) cache will take care of it. There are a few ways of doing this, some require working with the registry to find the location of the actual file
Clearing Outlook Most Recently Used Lists
your first address is correc

C# WPF how to send mail with attachment

In my WPFapplication I need to be able to open the default email to send an email with attachment previously scanned.
For the scanner i used WIA and I save the scanned image in jpeg.
For email i has tried the MAILTO as follows
string mail1 = "abc#hotmail.com";
string mail2 = "def#yahoo.it";
string attach = #"C:\bla\bla\bla\file.xlsx";
Process.Start(string.Format("mailto:{0}?subject={1}&body={2}&CC={3}&attachment:{4}"
, mail1, subject, body, mail2,attach));
every thing works unless ATTACHMENT when Process.start opens the default email.
you know something better than mailto? or something that gives the ability to attach files to email?
You can upload that file to a server, get a URL and include it into mailto body as a link.

Selenium Webdriver automation with emails

I'm currently trying to use Selenium Webdriver (C#) to automate a Forgot Password -> Reset Password workflow, where the user navigates to a page and supplies their username, and the backend code validates the username, then sends an email with a reset password link to the email address associated with their account.
I'm able to automate the process up to the point where the code sends the email, but I don't know any ways of checking for the email and/or clicking a link in the email, so I was hoping someone more experienced with Selenium/automation may be able to give me a few pointers.
Ideally the test should not care about the email address that the email is being sent to. Is there a way for Selenium WebDriver or some 3rd party package to catch the email being sent?
Thanks for any input or suggestions.
No. You are talking about setting up an email server, which is not an easy task.
You should send it to a test work email (if this is for a company), or a public email (hotmail/gmail), or if security is not an issue at all, the easiest place to send it would be a disposable email (mailinator)
You could try PutsBox. You can send an email to whatever-you-want#putsbox.com, wait for a few seconds (SMTP stuff ins't instantaneous) then check your email via http://preview.putsbox.com/p/whatever-you-want/last.
Have a look at this post tutorial, it can give you some ideas.
There is no integration of selenium with email clients like Thunderbird/Outlook. But if you have a web interface of the same email client, then you can access the email from browser and using selenium you can read and respond to the emails. I have tried this recently and it works fine where I have used web Outlook for testing.
Hope this helps.
Hi I was in a similar situation and was able to successfully implement a way to get an activation or forgotten password links.
Using Java Mail API I was able to trigger a method when such action is performed which goes into a Folder and read a specific message line then get the link and open it up in a browser using WebDriver.
However the main drawback with this is the inconsistency of reading a specific folder, sometimes emails goes to spam or other folder (in case of Gmail the new Social Folder) making it invisible or difficult to be retrieved.
Overall i think its a process that shouldn't really be automated, In terms of testing it should be done more code base level by mocking responses.
Snippet below should give you an idea on how to go about implementing
public class RefactoredMail {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "username", "password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());
System.out.println("Activation Link:" + ((String)
bp.getContent()).startsWith("http"));
String [] line = new String[1];
line [0] = mp.getContentType().toString();
System.out.println("Activation Link:" + (mp.getBodyPart(0).getLineCount()));
System.out.println("Activation Link:" +line[0]);
} catch (Exception e) {
e.printStackTrace();
}
//WebDriver Stuffs
public String activationUrl() {
//getting the url link and making it a global variable .... etc
//Accessing the link
}
}
You can use https://github.com/cmendible/netDumbster or http://ndumbster.sourceforge.net/default.html. I've used one i forget which. This will host an smtp listener and allow you to make assertions against any email it receives. Its kind of awesome! The caveat is you need to be able to control where the server delivers mail in the environment you are testing.

To open outlook new email window with content already filled

How can I implement email functionality, where:
When I click on an asp button, a new email window opens with all the content already added from code behind( including To address, From address, Subject and Body).
But the email should not be sent automatically. It requires user to click send button.
The purpose is, admin can modify the email content before sending to users.
Has anyone worked on similar functionality and can help or give me idea on how to implement it ?
Outlook.Application outlookApp = new Outlook.Application ();
Outlook._MailItem mailItem = (Outlook._MailItem)outlookApp.CreateItem ( Outlook.OlItemType.olMailItem );
mailItem.To = address;
// body, bcc etc...
mailItem.Display ( true );
I think you do not need ASP.NET for this. All you need is mailto with the right parameters.
Example:
<a href="mailto:someone#example.com?Subject=Hello%20again&body=This%20is%20body">
Send Mail</a>
This will send email to someone#example.com with subject "Hello" and body "This is body". You can also use CC parameter to add CC emails.
More info on this link
A related question that may help How do I properly encode a mailto link?

C#: What is the best method to send support request via email?

I have a windows forms application that I am adding a request support form to, and would like the user to be able to input the values and hit a button. Once the button is pushed I can either:
Open a new mail message and auto populate the message. (Not sure how to do this)
Submit the request via a http form on my website. (I know how to do this)
Send an email directly from the code of the application. (I know how to do this)
What I want to know is what would be the best method to use? I think option 1 is the most transparent, and the user will see exactly what is being sent, but I am not sure how to ensure it works no matter what email client they use.
I see there being potential issues with option two, specifically a firewall possibly stopping the submission. But option 2 would allow me to supply them with a ticket number right then and there for their request.
Thanks for the help.
For Option 1, as suggested, use the mailto handler.
Format your string like so: string.Format("mailto:support#example.com?subject={0}&body={1}", subject, body). Don't forget to UrlEncode the subject and body values.
Then use System.Diagnostics.Process.Start() with your string.
This will launch the registered mail handler (Outlook, Windows Live Mail, Thunderbird, etc) on the system.
For option 1 : If the message body is short, then invoking the mailto handler from inside your code no longer requires that they be using outlook. It's kinda a cheap hack, but it's completely cross-platform for local mail clients. (If they're using something like gmail, you're still SOL, though)
Option 2) is the best to avoid enterprise firewall issues because the HTTP port may not be blocked.
Option 2) is the best for simple configuration. The only config key you will have is the service/page url. Then your SMTP configuration will stay on your webserver.
Now you will have to choose between using a webpage (if one already exists) or a webservice (which is best fitted for your feature).
For option (1) be prepared to deal with Outlook version problems. But this is not hard (again if we are talking about Outlook, last version)
//using Microsoft.Office.Interop.Outlook;
private void OutlookMail(string Subject, string Body)
{
ApplicationClass app = new ApplicationClass();
NameSpaceClass ns = (NameSpaceClass)app.GetNamespace("mapi");
ns.Logon("", "", true, true);
MailItem mi =
(MailItem)app.CreateItem(OlItemType.olMailItem);
mi.Subject = Subject;
int EOFPos = Body.IndexOf(char.Parse("\0"));
if (EOFPos != -1)
{
log.Error("EOF found in Mail body");
ErrorDialog ed = new ErrorDialog(TietoEnator.Common.ErrorDialog.ErrorDialog.Style.OK, "Export Error", "File could not be exported correctly, please inform responsible person", "", "EOF char detected in the body of email message.");
ed.ShowDialog();
Body=Body.Replace("\0", "");
}
mi.HTMLBody = "<html><head><META content='text/html; charset=CP1257' http-equiv=Content-Type></head><body><table>"+Body+"</table></body></html>";
mi.BodyFormat = OlBodyFormat.olFormatHTML;//.olFormatPlain;
mi.Display(0); // show it non - modally
ns.Logoff();
}
BTW for automatic support requests I plan to use in my current project "Microsoft Enterprise Logging Support Block" email sending functionality.

Categories