How can I send emails using my own mail server in Meteor? - email

I am trying to enable email support for my Meteor application, and since I have my own server I want to also use my own mail server. So I installed postfix in my Debian wheezy server and successfully sent and email to my GMail address, so that means the mail server works properly and sends emails.
When I deploy my Meteor app and try to send an email though, say to do a password reset, my app crashes with the following error:
Exception while invoking method 'forgotPassword' RecipientError: Can't send mail - all recipients were rejected
at Object.Future.wait (/home/loupax/phial/bundle/programs/server/node_modules/fibers/future.js:326:15)
at smtpSend (packages/email/email.js:94)
at Object.Email.send (packages/email/email.js:155)
...
...
My MAIL_URL environment variable is in the format MAIL_URL=smtp://my_domain.tld.

Looks like all I had to do, is change the MAIL_URL environmental variable from smtp://my_domain.tld to smtp://localhost. After that everything worked just fine

Is your server on Amazon? Sometimes SMTP servers are known to block anything sent from certain hosting providers entire IP ranges to block spam.
You might want to consider using a different SMTP server, Amazon's SES, or Mandrill (which has a meteorite package to help) (personally I use both SES and Mandrill).
Note its not only Amazons IP blocks which are in this, its also any hosting provider a spammer can quickly set up. Your SMTP sever probably employs a list from somewhere with all these ips on it

for forgot password email, follow below steps
1) create smtp.js file in server folder and past below code in it
Meteor.startup(function () {
process.env.MAIL_URL =
'smtps://abcd#gmail.com:password#smtp.gmail.com:465';
});
2) paste below code in forgot password.js file
Template.forgot.events({
'click #forgot'(event,template) {
event.preventDefault();
let email = $("#email").val();
// paste below code in server.main.js -> in Meteor.startup function.
/* Accounts.urls.resetPassword = function(token) {
return Meteor.absoluteUrl('reset-password/' + token);
};*/
Accounts.forgotPassword({email:email},function (error,result) {
if(error)
{
alert(error);
}
else
{
console.log(result);
alert("mail sent ..!! Check your mail box");
FlowRouter.go('/login');
}
});
}
});
3 ) in main.js file in server folder paste below code
import '../server/smtp';
Meteor.startup(() => {
// code to run on server at startup
Accounts.urls.resetPassword = function(token) {
return Meteor.absoluteUrl('reset-password/' + token);
};
});
check your mail

Related

Liferay 7 MailService.sendMail from a custom portlet doesn't work only on HOST SERVER

I have a strange problem with a custom portlet on Liferay 7 to solve:
MailService.sendMail is working from MY COMPUTER with google smtp and a personal account: this means that the code is working...
On my HOST SERVER sending emails with the final-smtp works correctly: I tried both root and liferay user with telnet final-smtp port.
On the Liferay server on HOST SERVER, send e-mails works correctly: if I forget the password, Liferay send me the e-mail.
But ... if I try to send email with my portlet from Liferay on HOST SERVER it doesn't work without any error. I'm using Liferay MailService.sendMail. I post the code but it works (on MY COMPUTER).
I get the service in this way:
#Reference(unbind = "-")
protected void setMailService(MailService mailService) {
_mailService = mailService;
}
And the calling code is in the following:
InternetAddress fromAddress = null;
String newsletterPrefix = null;
InternetAddress toAddress = null;
try {
String smtpUser = PropsUtil.get(
"newsletter.send.mail.smtp.user");
String smtpToUser = PropsUtil.get(
"newsletter.send.mail.smtp.to.user");
if (Validator.isNotNull(smtpUser)) {
fromAddress = new InternetAddress(smtpUser);
}
if (Validator.isNotNull(smtpToUser)) {
toAddress = new InternetAddress(smtpToUser);
}
}
catch (Exception e) {
_log.error(e, e);
result = false;
}
MailMessage mailMessage = new MailMessage(
fromAddress, toAddress, subject, body, true);
mailMessage.setBCC(addressList);
_mailService.sendEmail(mailMessage);
There could be several reasons behind this, some not even code related.
I see you are using this code for a newsletter, which suggests you are using a smtp service that is meant for this.
It could be that your server is in fact sending the email, with success, but the smtp server is simply blocking, rejecting or marking to resend later. Moreover, that server might be configured to not send an error message, or sending an error message in the form of a successful delivery, but the data contains the error.
I would start checking you mail server configuration, and the accounts permissions, then its logs.
Also, you might consider using plugins for mass mail delivery, like this one: https://www.e-systems.tech/blog/-/blogs/connecting-liferay-to-mailgun
Few things you can do to debug this problem:
Make sure you are deploying the intended code on HOST machine. (Silly suggestion, but many times this is the problem.)
Try to set following package's Log level to ALL/DEBUG to see if it shows any problem in logs.com.liferay.mail.service

Getting "Client was not authenticated to send anonymous mail during MAIL FROM" error from Exchange SMTP using Rust lettre library

I am writing a Rust application that will send email through an Exchange server with SMTP functionality enabled. According to Microsoft's webpage, the settings that are required are:
Server address smtp.office365.com
Port 587
StartTLS Enabled
Credentials for mail account login
These are corroborated by the POP/IMAP settings of the webmail service.
Here is my code (with some censoring):
extern crate lettre;
use self::lettre::email::EmailBuilder;
use self::lettre::transport::smtp::{SecurityLevel, SmtpTransportBuilder, SUBMISSION_PORT};
use self::lettre::transport::smtp::authentication::Mechanism;
use self::lettre::transport::EmailTransport;
pub fn send_mail() {
let email = EmailBuilder::new()
.from("my email")
.to("destination email")
.body("testing")
.subject("testing")
.build()
.unwrap();
// Connect to SMTP server
let mut transport = SmtpTransportBuilder::new(("smtp.office365.com", SUBMISSION_PORT))
.expect("Failed to create email transport")
.encrypt()
.smtp_utf8(true)
.credentials("my email", "my password")
.authentication_mechanism(Mechanism::Login)
.build();
println!("Mail transport built");
println!("{:?}", transport.send(email.clone()));
}
When I compile and run the code,it gives me this error:
Err(Permanent(Response { code: Code { severity:
PermanentNegativeCompletion, category: Unspecified3, detail: 0 },
message: ["5.7.57 SMTP; Client was not authenticated to send anonymous
mail during MAIL FROM [SYXPR01CA0106.ausprd01.prod.outlook.com]"] }))
Why is this happening?
The closest I've come in my research is an issue on GitHub in relation to the lettre library not supporting the Login authentication mechanism (which Office 365 uses); however, the codebase was updated to support Login and I am using the master branch directly from GitHub so theoretically my application should support the Login mechanism.
Edit: Forgot to mention that I attempted an EHLO to the server, but it returned a (Client:(Connection closed)) error.
I used telnet and openssl to try connecting directly to my SMTP server, where I found that AUTH LOGIN requires 3 commands; one to send the AUTH LOGIN code, one to send the username and another to send the password. I found that the lettre library implements all its AUTH commands as single commands, so this wasn't working with the server. I downloaded the source code for the library, changed the send function to do the three separate commands, recompiled my code and everything worked fine :)
My addition to the lettre code:
if (accepted_mechanisms[0] == Mechanism::Login) &&
(accepted_mechanisms.capacity() == 1) {
try_smtp!(self.client.command("AUTH LOGIN"), self);
try_smtp!(self.client.command(base64::encode_config(
&username.as_bytes(),
base64::STANDARD).as_str()), self);
try_smtp!(self.client.command(base64::encode_config(
&password.as_bytes(),
base64::STANDARD).as_str()), self);

gerrit sendemail "Server rejected body"

After I configured my gerrit server with our SMTP server, I can not send email, can anyone help?
I was trying to config my gerrit with SMTP server, after I checked with command "gsasl --smtp -a --connect=10.4.103.110:25 -p passwrod", I have confirmed that my connection with SMTP server is correct.
After I configured it into gerrit.config, and restart gerrit. I tried to modify my first account's contact information by the webUI, I met a problem like "server xxxx rejected body".
Then I checked with log, and found the line is at 204 in the file "SmtpEmailSender.java", the code is like :
w = new BufferedWriter(w);
for (Map.Entry<String, EmailHeader> h : hdrs.entrySet()) {
if (!h.getValue().isEmpty()) {
w.write(h.getKey());
w.write(": ");
h.getValue().write(w);
w.write("\r\n");
}
}
w.write("\r\n");
w.write(body);
w.flush();
w.close();
if (!client.completePendingCommand()) {
throw new EmailException("Server " + smtpHost + " rejected body");
}
I resolved this problem is because I tried to display my email address as a non-relative address.
My address was aaa#mymail.com, I tried to config the user as bbb#mymail.com, the SMTP server rejected my send email request.

.NET - Mail server doesn't send mail through SmtpClient.Send

I wrote a single console application (just a part of a site code, but it must work apart too, and it has the same fault result as inside the site) (C#):
MailMessage message = new MailMessage("login#ourDomenInPunycode", "toMail")
{
Subject = "Hello",
Body = "Hello world"
};
SmtpClient client = new SmtpClient();
client.Host = "ourIP";
client.Credentials = new System.Net.NetworkCredential("login#ourDomenInPunycode", "ourPassword");
client.Port = 25;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
So, isn't to send e-mail should be trivial? But wherever I send mail from local machine through our mail server (just running this console application), the following exception appears:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Relaying to denied (authentication required)
If I change the "login#ourDomenInPunycode" data to my own mailbox (at gmail or something else - no matter), all works fine. It also not depend from "toMail" address.
So, what could be wrong with our mail server? Any special settings? We use Windows Server 2008 virtualized inside another Windows Server 2008 and Kerio Connect 7 as mail server at virtual Windows Server 2008. All other mail programs like Outlook works well with sending e-mails from our mail server.
All articles which I read in Internet about SmtpClient's settings have only these (above) trivial settings and code, nothing special.
UPDATE
I done some fixes in text above.
Here is a part of log of our mail server when I tried to send mail through console application launched from the mail server virtual PC ("mail.ourDomen.local" related to "ourIP" above):
Task 215 handler BEGIN
Task 215 handler starting
SMTP server session begin; client connected from mail.ourDomen.local:49399
Sent SMTP greeting to mail.ourDomen.local:49399
Command EHLO OurMailServer
Sent reply to EHLO: 250 mail.ourDomenInPunycode ...
Command MAIL FROM:<login#ourDomenInPunycode>
Sent reply to MAIL: 250 2.1.0 Sender <login#ourDomenInPunycode> ok
Command RCPT TO:<toMail>
Sent reply to RCPT: 550 5.7.1 Relaying to <toMail> denied
Connection to SMTP server mail.ourDomen.local lost: connection closed by remote host.
SMTP server session end
Task 215 handler END
"Sent reply to RCPT: 550 5.7.1 Relaying to denied" -
Why this happened?
Well, we use this description
https://kb.kerio.com/article/550-571-relaying-to-email%40addresscom-denied-authentication-required-411.html .
Although we know about this settings, but we tangled with our virtual machines. We have a virtual machine for the web server and another one for the mail server. Permissions were configured for the mail server virtual machine only in the Kerio Connect, not for the web server. We just added permission for the virtual machine of the web server and the mail is sent normally.
And the "ourIP" in the
SmtpClient client = new SmtpClient();
client.Host = "ourIP";
is the IP of our virtual machine of the mail server. No settings of IP of the web server virtual machine in the SmtpClient object.
As suggested your mail server needs to be configured to allow "Relaying" over port 25. It is the "Relaying" setting/config you are looking for.
The idea/purpose behind "Relaying" is stop your server being (ab)used for sending spam.
Try the code without setting client.Host and client.DeliveryMethod properties.
//used this referances
using System.Net.Mail;
using System.Net;
using System.IO;
try
{
string em_from = "your seding e mail";
string em_to = Ricever e mail Address;
SmtpClient Smtp_Server = new SmtpClient();
MailMessage e_mailx = new MailMessage();
Smtp_Server.UseDefaultCredentials = false;
Smtp_Server.Credentials = new System.Net.NetworkCredential("sender email address", "sender passsword");
Smtp_Server.Port = 25; //your mail server port
Smtp_Server.EnableSsl = false;
Smtp_Server.Host = "192.XXX.XX.XX"; //your mail server IP
e_mailx = new MailMessage();
e_mailx.From = new MailAddress(em_from);
e_mailx.To.Add(em_to);
e_mailx.Bcc.Add("BCC Address");//you cad add both BCC and CC addresss
e_mailx.IsBodyHtml = false;
e_mailx.Subject = esub;
e_mailx.Body = ebody;
e_mailx.Attachments.Add(new Attachment(emsg)); //emsg mean attach file name with location
Smtp_Server.Send(e_mailx);
return 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return 0;
}

SASL authorization failing while connecting to XMPP server

I am trying to connect to gmail using SMACK API through XMPP server. but getting the
error : SASL authentication failed using mechanism PLAIN
you can check a glimpse of code. I got it from net only
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
connection = new XMPPConnection(connConfig);
connection.connect();
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
I checked in the smack debug window. it says in XML :
< invalid-authzid />
I am already having account on gmail and my gtalk is also running.
You need to set the authentication before you connect viz
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
must appear before connection.connect().
See my blog.
ConnectionConfiguration cc = new ConnectionConfiguration(
"vietnam.agilemobile.com", 5222, vietnam.agilemobile.com");
XMPPConnection connection = new XMPPConnection(cc);
try {
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.connect();
Log.e("LOGIN", "" + 111);
// You have to put this code before you login
Log.e("LOGIN", "" + 222);
// You have to specify your gmail addres WITH #gmail.com at the end
connection.login("nemodo", "123456", "resource");
Log.e("LOGIN", "" + 333);
// See if you are authenticated
System.out.println(connection.isAuthenticated());
} catch (XMPPException e1) {
e1.printStackTrace();
}
I also get this mistake, but i can not work.
For anyone looking for possible solutions to this many years after this was originally asked and answered, I recently was able to get past this authentication error by explicitly setting the authzid value on the XMPPTCPConnectionConfiguration.
I was running into an issue where my connection configuration worked fine for some client XMPP servers, but not for others, even though they were all using SASL PLAIN authentication. After some troubleshooting, I learned that the ones that were failing were expecting an authzid value. After adjusting my code to set this, it works in both the environments that were working before, as well as the environments that were failing.
Here is how I am building my connection configuration:
XMPPTCPConnectionConfiguration.builder()
.setHost(XMPP_DOMAIN)
.setXmppDomain(XMPP_DOMAIN)
.setPort(XMPP_PORT)
.setCompressionEnabled(true) // optional, not all servers will support this
.setUsernameAndPassword(XMPP_USER, XMPP_PASSWORD)
.setResource(XMPP_RESOURCE)
.setAuthzid(JidCreate.entityBareFrom(String.format("%s#%s", XMPP_USER, XMPP_DOMAIN))) // <-- this was the change I needed
.build();
Specifically I needed to add this line:
.setAuthzid(JidCreate.entityBareFrom(String.format("%s#%s", XMPP_USER, XMPP_DOMAIN)))