Username is getting appended to From address while sending mail using Mailkit - email

I am using the following code for sending mail using mailkit in dot net core.
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "noreply#localhost.com"));
message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "mymail#gmail.com"));
message.Subject = "How you doin'?";
message.Body = new TextPart("plain"){ Text = #"Hey" };
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587);
client.Authenticate("mymail#gmail.com", "mypassword");
client.Send(message);
client.Disconnect(true);
}
I am using smtp client which requires authentication.
Once i receive a mail, the FROM address in that mail is mymail#gmail.com which is the username used to connect to that host, but not noreply#localhost.com which is actual FROM address.
The other option is username gets appended to the actual FROM address.
Example- noreply#localhost.com[mailto:mymail#gmail.com]
Can someone provide a solution to control this?

This is not a problem with MailKit, it's simply a feature of GMail. This is an attempt by Google to prevent spoofing emails.

Related

aspnetboiletplate - send email with attachments

I have an aspnetboilerplate template, .netcore & angular (free version). I am trying to find a way to attach a word document to an email using the IEmailSender but cant find the proper way of doing so. Have already checked the Email Sending but there is no hint for attaching file to an email.
Does anyone have a sample code that could possibly share with me?
Here is the code snippet to send an email with attachment:
MailMessage mail = new MailMessage
{
Subject = "Subject",
Body = "Message",
IsBodyHtml = true,
To = { "toaddress#gmail.com"},
From = new MailAddress("fromaddress#gmail.com")
};
mail.Attachments.Add(new Attachment(_env.WebRootPath + "\\pp.jpg"));
_emailSender.SendAsync(mail);
You can directly construct a MailMessage and pass it to IEmailSender.SendAsync(mailMessage).
See https://github.com/aspnetboilerplate/aspnetboilerplate/blob/94ebd48fd959cd460d97b809317a959e45c94067/src/Abp/Net/Mail/EmailSenderBase.cs#L66
If you are using MailKit, the underlying implementation will convert the Mail message object into MimeMessage and send it via Mailkit
See https://github.com/aspnetboilerplate/aspnetboilerplate/blob/94ebd48fd959cd460d97b809317a959e45c94067/src/Abp.MailKit/MailKitEmailSender.cs#L42-L50
MimeMessage implementation
https://github.com/jstedfast/MimeKit/blob/bcc7030b61c0c83a10eab7e7a5d689efd923038d/MimeKit/MimeMessage.cs#L3494

ccing using SendGrid Service in Bluemix

Trying to use SendGrid Service in Bluemix coding in Node.js. I use the addCc() method to add an address to cc to. I get no error msg and the mail is delivered to the main address, but nothing gets sent to the cc:ed address. And if I look ath the top of the mail going to the main recipient I can see the cc address there. Does anyone know if there is a bug or limitation in using cc with SendGrid?
Best Regards
W
A common error is to pass an array to the addCc() function when it expects a string. Using v2.0.0 of the 'sendgrid' npm module, the code below will correctly send an email which cc's 'jennifer#electric.co'.
As mentioned in the comment above, verify that you're not hitting issue https://github.com/sendgrid/sendgrid-nodejs/issues/162
// Pre-req: get the SendGrid credentials for username and password
// from VCAP_SERVICES into the 'user' and 'pass' vars
var sendgrid = require('sendgrid')(user, pass);
var email = new sendgrid.Email({
to: 'fargo.north#electric.co',
from: 'bronco.bruce#electric.co',
subject: 'SendGrid Test',
text: 'This is a SendGrid test'
};
// add a cc address as a single string
email.addCc('jennifer#electric.co');
sendgrid.send(email, function(err, json) {
if (err) {
return console.error(err);
}
console.log(json);
}

Does IMAP search work on outlook.com

I'm new to IMAP.
I'm trying to fetch mails from outlook.com, I have configured outlook.com IMAP settings in my email client. I'm able to connect and get messages from outlook.com.
But when I'm trying to get messages by using search term like
I'm using Java mail packages.
IMAPStore store = getStore(serverName, userName, password, port);
IMAPFolder inboxFolder = (IMAPFolder) store.getFolder("INBOX");
SearchTerm[] searchTerms = new SearchTerm[4];
Address address = new InternetAddress("search_email_address");
SearchTerm toTerm = new RecipientTerm(Message.RecipientType.TO, address);
SearchTerm ccTerm = new RecipientTerm(Message.RecipientType.CC, address);
SearchTerm bccTerm = new RecipientTerm(Message.RecipientType.BCC, address);
SearchTerm fromStringTerm = new FromStringTerm(searchEmail);
searchTerms[0] = toTerm;
searchTerms[1] = ccTerm;
searchTerms[2] = bccTerm;
searchTerms[3] = fromStringTerm;
OrTerm orTerms = new OrTerm(searchTerms);
inboxFolder.search(orTerms);
I'm trying to search mails using like above search terms in a IMAP folder, but I'm getting zero mails.
Any API limitations in this or any problems in code, please guide me
Thanks
Ramesh
Looking at the question "Does IMAP search work on outlook.com" I can say: Yes.
I'm using imapsync to sync from outlook.com to other mailboxes and it works. Behind the scenes imapsync uses IMAP searches.
The IMAP capabilities announced by outlook.com are:
IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+ AUTH
Maybe this helps somebody. This could also explain why certain search keywords might or might not work.

Microsoft.Exchange.WebServices.Data.ServiceResponseException: When Sending Emails

I am using EWS Managed API to send email.
I am getting a Microsoft.Exchange.WebServices.Data.ServiceResponseException: EmailAddress or ItemId must be included in the request.
In the Soap return XML I see ErrorMissingInformationEmailAddress : This error occurs if the EmailAddress (NonEmptyStringType) element is missing.
Which email address is it talking about?
Using Exchange 2007 SP1.
Exchange credentials are correct and the to/from email addresses are valid emails.
Any ideas? Google has not helped.
Same code has worked for other Exchange Servers.
service.AutodiscoverUrl() does not work for this server.
using Microsoft.Exchange.WebServices.Data;
protected void SendEwsMail()
{
//Trust all certificates
System.Net.ServicePointManager.ServerCertificateValidationCallback =
((sender, certificate, chain, sslPolicyErrors) => true);
var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new NetworkCredential("user#domain.com", "password");
service.Url = new Uri("Url");
var email = new EmailMessage(service);
email.ToRecipients.Add("user#domain.com");
email.From = new EmailAddress("user#domain.com");
//email.ReplyTo.Add(recipient.FromAddress);
email.Sender = new EmailAddress("user#domain.com");
email.Subject = "test";
// Send the message and save a copy.
email.SendAndSaveCopy();
}
It turns out that for the mail Server (MS Exchange) in question I needed to use this method:
Writing an encrypted mail via Exchange Web Services
var item = new EmailMessage(service);
item.MimeContent = new MimeContent(Encoding.ASCII.HeaderName, content);
// Set recipient infos, etc.
item.Send();
It seems to be because of the encrypyed MIME attachment. Using the standard To, From, Subject properties of the Microsoft.Exchange.WebServices.Data.EmailMessage class does not work correctly.
Although it does work as expected when the mail server was SmarterMail.
SmarterMail 9.x is one of the only mail servers (including Microsoft Exchange) to support Exchange Web Services (EWS).
(from http://blogs.smartertools.com/tag/exchange-web-services/)
Anyone know why SmarterMail would behave differently to MS Exchange?

Sending Email in DNN

I'm trying to send an email in a DNN module I'm making. However, though it doesn't crash the email isn't being sent. I think it has to do with the From Email I'm attempting to use. I'm not 100% sure what email I should be using for the from which is the first parameter.
Protected Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
DotNetNuke.Services.Mail.Mail.SendEmail("support#localhost", "myemail#site.com", "EmailTest", "Hello world!")
End Sub
The More likely problem is you don't have your SMTP settings properly configured. To configure your SMTP settings, Login as Host. Then, go to Host -> Settings and fill out the fields under "SMTP Server Settings" and save them. There's a test link there as well to verify they are working correctly.
This is probably pretty late to the party, but I often use the Mail.SendMail() method, and then manually pass all the STMP information like below, and then when debugging I check the message that comes back. (As of DotNetNuke 5.5)
Dictionary<string, string> hostSettings = HostController.Instance.GetSettingsDictionary();
string server = hostSettings["SMTPServer"];
string authentication = hostSettings["SMTPAuthentication"];
string password = hostSettings["SMTPPassword"];
string username = hostSettings["SMTPUsername"];
// using the Mail.SendMail() method allows for easier debugging.
var message = Mail.SendMail(from, user.Email, String.Empty, subject, body, String.Empty, "HTML", server, authentication, username, password);
Late to the game as well, but I just ran into a similar issue earlier today...
The DNN sendMail or sendEmail method handle the exceptions on their own, and add it to their DNN logs. Unfortunately, they never return said exceptions to the main code where you are calling the functions - hence why your code executes just fine!
You can look further into their exceptions table, or Admin Logs in the UI, for more info on the particular issue you are having.
I changed my code to use System.Net to send emails and collect all of the info you need from DotNetNuke.Entities.Host.Host object in DNN. This way, we can handle the error and have our code work around it :) I ended up with something like this (it's in c# but you can do the same in VB.Net with a slightly different syntax):
//make the email
MailMessage mail = new MailMessage("From#me.com","to#a.com,to#b.com,to#c.com");
mail.Subject = "test subject";
mail.Body = "actual email";
string dnnServerInfo = DotNetNuke.Entities.Host.Host.SMTPServer;
// The above looks like "server.com:port#", or "smtp.server.com:25"
//so we find the colon to get the server name, and port using the index below
int index = dnnServerInfo.IndexOf(':');
//make the SMPT Client
SmtpClient smtp = new SmtpClient();
smtp.Host = dnnServerInfo.Substring(0, index);
smtp.Port = Int32.Parse(dnnServerInfo.Substring(index + 1, dnnServerInfo.Length - index - 1));
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(DotNetNuke.Entities.Host.Host.SMTPUsername, DotNetNuke.Entities.Host.Host.SMTPPassword);
smtp.EnableSsl = DotNetNuke.Entities.Host.Host.EnableSMTPSSL;
//send the email
smtp.Send(mail);
I used part of the original code from "SendMail" found here to come up with this: https://stackoverflow.com/a/19515503/6659531
Good luck to anybody who comes across this :)