Sending E-Mail through app causes it to crash - c#

I am using ShowComposeNewEmailAsync().
EmailRecipient sendTo = new EmailRecipient()
{
Address = "example#example.com"
};
//generate mail object
EmailMessage mail = new EmailMessage();
mail.Subject = subject.Text;
mail.Body = body.Text;
//add recipients to the mail object
mail.To.Add(sendTo);
//open the share contract with Mail only:
await EmailManager.ShowComposeNewEmailAsync(mail);
The app crashes in phone when I have more than 1 app(I had multiple Google Mail Account IDs logged in in email+accounts i.e. Google Mail,Google Mail 2,Google Mail 3) to do this or have no email set in email+accounts in Settings.
When I call this function and have more than 1 account, the app selector appears for 2 or 3 seconds (showing Google Mail,Google Mail 2,Google Mail 3 in the list) and then disappears before I can select anything closing the app along with it.
If no accounts are available then a message appears saying no apps are installed to share for 1-2 seconds and then it disappears closing the app along with it.
How to prevent this from happening?

Can you try with simple values? This works fine for me, and I have several e-mail accounts on my phone:
var msg = new EmailMessage();
msg.Subject = "Hello this is a test";
msg.Body = "Testing 123";
msg.To.Add(new EmailRecipient("address#somewhere.com"));
await EmailManager.ShowComposeNewEmailAsync(msg);
Do you have your code wrapped in a try/catch in case you are getting an exception that is being lost during the async call?

Related

Sending mail with MimeKit and default email

I need send a mail from my program. Then I use MimeKit.
The problem is when run the program in another computer, with another email configurated by default.
So I need to use that default email on that computer.
My code is
var message = new MimeMessage();
message.From.Add(new MailboxAddress("CCCC", "cccc#xxxxx.com"));
message.To.Add(new MailboxAddress("KKKK", "kkkkkk#jjjjj.es"));
message.Subject = "¿Cómo estás?";
message.Body = new TextPart("plano")
{
Text = #"Kaixo Lorea"
};
using (var smtpClient = new SmtpClient())
{
smtpClient.Send(message);
}
I'm working with C# and Windows 10
Thanks
As far as I understand your question, you are asking how to read the SMTP server configuration settings from whatever email client program is configured on another system.
I doubt you'll have much luck with that.

Outlook Email won't display in web application

I have a web application with some functionality to allow a user to send an email using Microsoft.Office.Interop.Outlook.Application. I want to display the email to allow the user to add any text they may want before sending. It works as expected in localhost however in production it fails to display the email. My code is as follows:
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.To = address;
mailItem.Subject = subject;
mailItem.HTMLBody = body;
mailItem.Importance = OlImportance.olImportanceNormal;
mailItem.Display(false);
Outlook opens just fine when I use a Response.Redirect instead of the above.
Response.Redirect("mailto:" + email + "?subject=" + subject + "&body=" + body);
Any ideas/suggestions?
Create an EML (MIME) message with attachments etc. on the server and let the user download it. Outlook on the client side will be happy to open it. Don't forget to add the X-Unsent:1 MIME header to make sure the user can actually send it.

How to send email directly to user using C# in Windows 10 Apps?

I have following code
EmailMessage email = new EmailMessage();
email.To.Add(new EmailRecipient("xyz#live.com"));
email.Subject = "Msg Subject ";
email.Body = "My Msg";
await EmailManager.ShowComposeNewEmailAsync(email);
showComposeNewEmailAsync() launches the email application with above message displayed,
but I want to sent the email directly to the user without launching the email application. How can I do it?
As I am new to coding please explain me in details.
You have to work with Sockets and implement the SMTP behavior by yourself.
Otherwise you could also use a SMTP client for WinRT. According to the Microsoft Forum this should also work with UWP.
Use SMTP client for Windows Store as follows:
First install this Nuget Package:
Install-Package lightbuzz-smtp
Second include namespace
using LightBuzz.SMTP;
Then you can use this code snippet to send email directly from UWP app:
using (SmtpClient client = new SmtpClient("smtp.example.com", 465, false, "SenderEmail#example.com", "YourPassword"))
{
EmailMessage emailMessage = new EmailMessage();
emailMessage.To.Add(new EmailRecipient("someone1#anotherdomain.com"));
emailMessage.CC.Add(new EmailRecipient("someone2#anotherdomain.com"));
emailMessage.Bcc.Add(new EmailRecipient("someone3#anotherdomain.com"));
emailMessage.Subject = "Subject line of your message";
emailMessage.Body = "This is an email sent from a UWP app!";
await client.SendMailAsync(emailMessage);
}

Send an email from the logged in user ASP.NET

I have created a webform in visual studio. This application is sending an email.
BUT I want the FROM address to be the logged in user's email. So for example, if John is logged in with his email and password and he clicks submit, the email will send to Person X but the from address will come from John. I hope this is a clear enough explanation.
This is the code snippet of my sending mail method.
{
MailMessage message = new MailMessage();
message.To.Add(nameddl.Text);
message.From = new MailAddress("");
message.Subject = "Notification";
message.Body = "An new entry has been made that requires your attention.";
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
}
So this line, message.From = new MailAddress(User.LoggedIn); this is what I want. BUT how do one code it to work like that, can it be made possible?
NB: This is NOT a MVC Application, just a regular ASP.NET Web form.
**EDIT Still struggling with this one, so if anyone got an idea that would be great?
If you are storing email in session then you can directly pass it like.
message.From = new MailAddress(Session["Email"].Tostring);
if this is not the case and you have stored email in database then you can get it from there by simply making one method that return email address.like
message.From = GetEmail();

How to send email notification to newly created user in Active Directory using ASP.net C#

I have implemented a web application which manage all the operations(User Creation, User modification, User termination) on Active directory(#server.local).
I want to send a email from #server.local domain when an user creation has done, we need to notify the manager of the user.
Now the problem is, I am not sure about the SMTP configuration settings on that AD server and I have tried a method to send a mail. But It does not send the mail successfully. Its showing error message like Failure sending mail.
My Code is:
string SMTPHost = "10.26.60.350";
string fromAddress = "Admin.User#Server.local";
string toAddress = "Test.User#Server.local";
System.Net.Mail.MailMessage notMess = new System.Net.Mail.MailMessage();
notMess.To.Add(toAddress);
notMess.Subject = subject;
notMess.From = new System.Net.Mail.MailAddress(fromAddress);
notMess.Body = bodyParams;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(SMTPHost);
smtp.Send(notMess); // Here I am getting the exception
How to resolve this propblem? Do we need to configure any settings on AD server to send mail by using #server.local domain? Please help me on this and suggest me about this.Thanks

Categories