Spring mail MimeMessage has an incorrect "From " set - spring

I am using spring mail to send an email via google's smptp server. I am setting my email templates "From" header but for some reason when I do receive the mail as a sender I get the owner of the smtp account. (which happens to be me again).
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setText(forgottenPassowrdMailTemplate.getText()
.replace("%firstName%", token.getUser().getFirstName())
.replace("%lastName%", token.getUser().getLastName())
.replace("%link%", url + token.getToken()), true);
helper.setTo(token.getUser().getEmail());
helper.setFrom(forgottenPassowrdMailTemplate.getFrom());
helper.setSubject(forgottenPassowrdMailTemplate.getSubject());
am I forgetting something ? I a am explicitly setting the "From" header

You are setting a from address that is different from the account's address. There are security measures by Google to avoid abuse, which could be fatal if you could just send with any arbitrary from address via Google's SMTP server. You need to link and verify your other account with the account you want to send the mail with. See here. Your original email address will still be available in the headers and visible to the receipient.
But why don't you just use the other accounts credentials (and mail server, if it is not a Google account)?

Related

Can I send email with mailgun sandbox domain under my local OS?

I registered myself at mailgun with domain I use for my laravel apps
Also I see that with my domain there is sandbox domain, which looks like :
sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
and under SMTP block I see text:
Grab your SMTP credentials:
SMTP hostname: smtp.mailgun.org
Port: 587 (recommended)
Username: postmaster#sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
Default password: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX
Working on my local OS, apache 2 under ubuntu 18
I try to send email in my local having in .env :
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster#sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
MAIL_PASSWORD=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS='from_email#site.com'
MAIL_FROM_NAME='FROM NAME'
MAILGUN_SECRET='XXXXXX-XXXXXXX'
MAILGUN_DOMAIN='sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org'
As result I sent email without errors, But I did not receive emails for 2 my users which are in Authorized Recipients of mailgun.
What for this sandbox domain, can I use it for local OS and can I send emails under my local OS ?
UPDATED :
In my local .env I set mailtrap.io params and it works ok.
In Account Security->API security->API keys of my mailgun account I have parameters L
Private API key
Public validation key
HTTP webhook signing key
Setting app on remote dev server
MAIL_MAILER=smtp
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=XXX
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS='from_email#site.com'
MAIL_FROM_NAME='FROM NAME'
If MAIL_USERNAME and MAIL_PASSWORD must be filled with values from Account Security->API security->API keys
also if MAIL_FROM_ADDRESS must one of Authorized Recipients I created in the settings before ?
UPDATED BLOCK 2:
I try to fill parameters from live domain, but failed.
I search paramters here : https://prnt.sc/18ouojv
?
Thanks in advance!
As Sandbox domains are restricted to authorized recipients only.So make sure to verify recipients emails in
https://app.mailgun.com/app/sending/domains
If recipients has not received email to inbox then check in spam
folder.
Ref:https://help.mailgun.com/hc/en-us/articles/360011702394-Why-Do-My-Emails-Go-to-Spam-
If still not working then change mailer to smtp
MAIL_MAILER=smtp
Then run
php artisan config:clear
Env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster#sandbox**************.mailgun.org
MAIL_PASSWORD=************************
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=youraccountemailaddress
MAIL_FROM_NAME="${APP_NAME}"
I would not use the sandbox domain, as it has problems, does not work like a real domain and there is no reason to not start with the real domain for free.
Short description to start with MailGun with your own domain:
Create an account at mailgun including a credit card
-> If you don't add a credit card, you will not be able to use MG
-> This don't means, that you have to pay unless you don't reach the limit
of 5'000 emails / month
add your (existing) domain in MG (only possible, if you have added a CC)
under "Sending" - button "Add new Domain" (Button only visible if a CC is added)
follow exactly the steps that are showed at the right top corner
in the MG portal. Title "Getting started"
You have to reach 100% before you will be able to send your first emails
Note:
This also includes "illogical" steps - e.g. send an email to mailgun#YourDomain
although, a mailbox "mailgun" does not exist
You further have to send an email to yourself
These steps seems to be needed for initial verification at MG
add exactly the DNS entries from the MG portal (spf and mta) to your domain
To do this you need access to the portal of your domain provider
in MG - under DNS Records - press button "Verify DNS settings"
the DNS settings have to be verified (a green check is showed), before you can send emails
If anything is O.K., you will receive an eMail from MG:
Good news! The DNS settings you added for YourDomain have been verifyed.
If you have received this email, you should be able to send emails over MG
Further notes to development (I work with Visual Studio 2017, Windows Forms and vb.net):
I had to set TLS 1.2 to be able to connect:
vb.net code:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
I further have added the NuGet package: RestSharp (as the code examples in MG are based on RestSharp).
Code example for vb.net (after adding the NuGet to RestSharp):
Imports System.Net
Imports System.Net.Mail
Imports RestSharp
Imports RestSharp.Authenticators
...
Dim client As RestClient = New RestClient()
client.BaseUrl = New Uri("https://api.eu.mailgun.net/v3/")
Dim cAPIKey As String = "YourKey from MG"
client.Authenticator = New HttpBasicAuthenticator("api", cAPIKey)
Dim request As RestRequest = New RestRequest()
request.AddParameter("domain", "YourDomain", ParameterType.UrlSegment)
request.Resource = "{domain}/messages"
request.AddParameter("from", "Your Description <Mailbox#YourDomain>")
' add mail address(es):
request.AddParameter("to", "mail-address1")
' further, if needed (Note: I send a single email to each recipient):
request.AddParameter("to", "mail-address2")
' add bcc mail address (if needed):
request.AddParameter("bcc", "bcc-mail-address")
' fill the mail:
request.AddParameter("subject", cMailBetreff)
request.AddParameter("html", cHTMLMailBody)
request.Method = Method.POST
Dim status As IRestResponse
status = client.Execute(request)
' state handling:
If status.StatusCode.ToString = "OK" Then
' O.K handling
Else
' Error handling
End If
Notes to the code above:
I send a complex html, that I have stored in a file that is loaded in the variable cHTMLMailBody.
After the load, some variables in the file are change to customer values stored on a SQL server (cMailBetreff also contains a customer specific generated text).
As I'm not from thee USA, I have to use the url: https://api.eu.mailgun.net/v3/".
If you are from the USA, you have to use another url (see MG portal).
Notes to the error handling in MG (Log in MG):
I have done some tests with the code above (that works).
As I also want to know (and store), if the sent email has reached the recipient, I have done some tests and sent emails to a not existing domain and an existing domain, but not existing mailbox.
To the not existing domain, an error was logged in MG (what is correct).
To the not existing mailbox, the state OK was logged in MG (what is wrong).
The reason to the wrong state (OK) to the not existing mailbox was, that MG don't receive non delivery mails, if there is no MX record to the domain that targets the MG server.
As we (manually) also send out emails to the domain from our own mail server, I don't wanted to change the mx record to our domain in general to the MG server.
Therefore I have added a sub domain to our domain (in MG and also at our DNS provider) and then added a MX record to the new sub domain that points to the MG server.
Then, I have changed the domain parameter to SubDomain.Domain:
request.AddParameter("domain", "SD.Domain", ParameterType.UrlSegment)
The problem then was, that a not nice "on behalf of" was generated automatically in the header.
I was able to solve the issue by adding a further parameter:
request.AddParameter("h:sender", "Your Description <Mailbox#YourDomain>")
(same value as in from parameter, whereby the h: is needed in addition)
After doing that, MG also receives non delivery emails and add also an error to not existing mailboxes.
So.. if you need to get/store the states to the sent emails and don't want to change your mx record generally to MG, you should add a sub domain (if not, you should not need a sub-domain).
For me, everything now seems to work as it should:
we can sed out html mails over MG
we can capture the states to the sent emails over the MG API
we still can send (manually) emails over our own mail server
And important:
if a recipient answers to an email, the answer is delivered to our mailbox on our server
Hope this helps somebody...

Laravel mailbox inbound parse sendgrid

So I'm trying to make my own inbox for emails using the followin packag beyondcode/laravel-mailbox
I installed the package on a blank project, in order to test it out.
I can send emails with the sendgrid API, so that's not a problem at all.
Now, I have some environment variables set;
MAILBOX_DRIVER=sendgrid
MAILBOX_HTTP_USERNAME=laravel-mailbox
MAILBOX_HTTP_PASSWORD=secret
The config just the same as given in the package.
In my AppServiceProvider, I have in the boot the following:
Mailbox::catchAll(static function(InboundEmail $email) {
// Handle the incoming email
Log::info('#-------------------------------------------------------------------------------------------#');
Log::info($email);
Log::info('#-------------------------------------------------------------------------------------------#');
});
But when I take a look in the logs, when I send an email to whatever#rallypodium.be, nothing hapens...
This is my DNS configuration:
And here is the inbound parse config on the sendgrid side:
What am I missing or doing wrong here? I looked at my access logs and not even a request is being made to the server from sendgrid...
So How can I make sure I can receive messages to whatever#mydomain.com?
Your DNS should also have a few CNAME records so that you can verify your domain with SendGrid.

Laravel Mail - Use special character in Sender's Name

I use Laravel 5.8.
And what I'm trying to do is to send an email with sender's name which contain special character such as TheTh#nos. And it is not working. The email was sent, but the sender's name will not displayed.
This is my code:
Mail::send('emails.purchase-token', $data, function($message){
$message->from('noreply#thetanos.com', 'TheTh#nos');
$message->subject('New Tokens Have Been Added To Your Wallet');
$message->to("recipient01#gmail.com);
});
I'm pretty sure this is because of how gmail (and most modern e-mail clients) work.
When you put a # symbol in the From header field, the email client thinks that you're attempting to spoof the mail to make it look like it originated from a different e-mail address, even though TheTh#nos isn't a valid e-mail address.
Try using other special characters in your from and see if that works.
Also, some hosting providers doesn't allow you to change the From header when using their SMTP server.

Why I When I using mailgun for sending from yahoo mail or sending to yahoo mail it not works?

when i using gmail email it can be reset and set the from recipents
but when using yahoo mail the set from doent send and reset to yahoo address was error with
Expected response code 250 but got code "554", with message "554 Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in Account Settings. "
Did you check out this page?
If this is the cause you could try sending an text-only e-mail for testing purposes, see if that does works.

JavaMailSender SMTP Bounce back - Different domain email address

I'm using the Spring java mailer class to send email messages to my users:
org.springframework.mail.javamail.JavaMailSenderImpl version 1.4 using Spring framework 3.0.7.RELEASE.
I want to set the bounce back message for a failed email to go to my user's email address that doesn't have the same domain as my smtp server. Does anyone know a way to accomplish this?
For example:
My system sends an email to email-does-not-exist#gmail.com. My smtp server is configured to have a domain somebusiness.com. Upon failure, send the bounceback to my user: test.user#gmail.com.
I read the following article several times:
Specifying the bounce-back address for email
I tried to use their method of setting the mail.smtp.from property but it won't send any emails at all (not even counting bounceback attempts from invalid emails yet).
Properties p = new Properties();
p.put("mail.smtp.from", "test.user#gmail.com"); //If I comment this out, it sends emails again
mailSender.setJavaMailProperties(p);
Session session = Session.getDefaultInstance(p, null);
MimeMessage mimeMessage = new MimeMessage(session);
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,
false, "utf-8");
mimeMessage.setContent(emailBody, "text/html");
helper.setTo(toAddress);
helper.setSubject(subject);
helper.setFrom(fromAddress);
mailSender.send(mimeMessage);
Anyone have an idea of why? The obvious answer seems like the smtp server we are using is blocking it but I was hoping for potential other ideas.
I'm having a similar problem. I don't have a solution yet, but at the moment I am considering replacing Spring's mail package with org.apache.commons.mail because it has a simple setBounceAddress(emailAddressString) method.
See the very end section "Handling Bounced Messages" of the user guide:
http://commons.apache.org/proper/commons-email//userguide.html
And the API docs:
http://commons.apache.org/proper/commons-email//apidocs/org/apache/commons/mail/Email.html#setBounceAddress(java.lang.String)
I just checked how the Apache Commons Mail implements it's bounce functionality and it actually just sets the from address. It means that you can do the same in Spring Mail by setFrom(...) on a org.springframework.mail.javamail.MimeMessageHelper class.
Source code snippet from org.apache.commons.mail.Email class:
if (this.bounceAddress != null) {
properties.setProperty(MAIL_SMTP_FROM, this.bounceAddress);
}
See it in the sources:
http://grepcode.com/file/repo1.maven.org/maven2/org.apache.commons/commons-email/1.2/org/apache/commons/mail/Email.java#539

Resources