Sending a Mail on Jenkins Pipeline - email

hi I have a simple pipeline job that should send a mail, I configured on Manage jenkins -> Configure System with port 465 and the appropriated SMTP server , when I test the configuration by sending a e-mail , it sends and I can see the test mail on my inbox , but from the pipeline job , it fails:
node {
stage("test") {
def message = "hello there!"
mail bcc: '', body: message, cc: '', from: 'jenkinsuser#mycom.com', replyTo: 'jenkinsuser#mycom.com', subject: "Develop: build generated ", to:'grace#mycom.com'
}
}
Error:
java.net.SocketException: Connection closed by remote host
at sun.security.ssl.SSLSocketImpl.checkWrite(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:114)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2104)
Caused: javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
java.net.SocketException: Connection closed by remote host
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2106)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2093)
at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1184)
Any suggestion how to fix it?
thanks,

I am using Email Extension plugin in scripted pipeline with no issues. Make sure you properly configured E-mail Notification section in Manage Jenkins > Configure System. Example for gmail:
Jenkins e-mail notifications settings
Plugin usage is described here: here. This is how I call it in Pipeline (I didn't remove variables):
emailext attachmentsPattern: "**/packages/rpm/splitted/${file}",
to: "${to}",
from: "Jenkins",
subject: "[jenkins] ${packageFullName}: part ${part}/${files.size()}",
body: "File received: \'${file}\'\n" +
"From package: \'${packageFullName}\'\n" +
"Package MD5: \'${mainMdFiveSum}\'\n" +
"Jenkins Build: \'${env.BUILD_URL}\'\n"

Related

Jenkins send e-mail notification to github committers

We want to send out e-mail notifications to our contributors if the Jenkins build was successful or not.
we are using the email extension plugin: https://plugins.jenkins.io/email-ext/
Notifications should be sent in the following cases:
The current build failed or is unstable
the current build is successful and the previous build failed or was unstable
I created a shared library with the following script
#!/usr/bin/env groovy
def call(String buildStatus = 'STARTED') {
buildStatus = buildStatus ?: 'SUCCESS'
def subject = "JENKINS NOTIFICATION - ${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def details = """${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"""
// Send email to user who has started the build
emailext(
subject: subject,
body: details,
attachLog: true,
compressLog: true,
recipientProviders: [[$class: 'RequesterRecipientProvider'], [$class: 'CulpritsRecipientProvider']]
)
}
In the Jenkinsfile I loaded the library shared-lib and I defined the sendNotification command in the post-block of the jenkinsfile:
#!groovy
#Library('shared-lib') _
pipeline {
agent any
stages{
stage('Checkout code base'){
steps{
checkout scm
}
}
stage('another stage'){
steps{
echo "do something"
}
}
}
post{
always{
sendNotifications currentBuild.result
}
}
}
Currently, the e-mail notifications are sent when I manually trigger a build at Jenkins, but when I push to GitHub and Jenkins build gets triggered, no e-mail notification is sent.
This is the log of the pipeline:
19:12:06 [Pipeline] emailext
19:12:06 Sending mail from default account using System Admin e-mail address
19:12:06 messageContentType = text/plain; charset=UTF-8
19:12:06 Request made to attach build log
19:12:06 Request made to compress build log
19:12:06 Adding recipients from project recipient list
19:12:06 Not sending mail to user noreply-github+ms#companyname.com with no permission to view NewSimple » MTEvent » master #203 Collecting change authors...
19:12:06 build: 203
19:12:06 adding author: noreply-github+ms
19:12:06 Not sending mail to user noreply-github+ms#companyname.com with no permission to view NewSimple » MTEvent » master #203Adding recipients from trigger recipient list
19:12:06 Successfully created MimeMessage
19:12:06 An attempt to send an e-mail to empty list of recipients, ignored.
PS: I don't know from where this email noreply-github+ms#companyname.com was pickup.
The correct email address should be myfirstname.lastname#companyname.com
Not sure what I have missed.
Any suggestions would be appreciated.

KeyCloak fails to send email using SMTP with status 500

I have Keycloak running in a Kubernetes cluster. Authentication works but I need to set up e-mail to be able to send e-mails for verification and password reset.
I have SendGrid set up as an SMTP Relay. These settings (host, port and api key) work when I send mail using the SendGrid java client. However, when pressing Test connection in KeyCloak I get:
[Error] Failed to load resource: the server responded with a status of 500 ()
[Debug] Remove message (services.js, line 14)
[Debug] Added message (services.js, line 15)
[Error] Can't find variable: error
https://<domain>/auth/resources/ong8v/admin/keycloak/js/controllers/realm.js:76 – "Possibly unhandled rejection: {}"
[Debug] Remove message (services.js, line 14)
There isn't much to go on here. I have an e-mail address set up for the currently logged in user. I've also tried resetting the password in case the Test connection functionality was broken but that didn't work either.
The Realm Settings settings user for email are as such:
host: smtp.sendgrid.net
port: 587
from: test#<domain>
Enable StartTLS: true
Username: "apikey"
Password: <api key>
Any idea what can be wrong? Or how to find out? For instance, maybe I can get a more meaningful error message somehow.
Edit:
I got the server logs.
Failed to send email: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.sendgrid.net, 587; timeout 10000;
nested exception is: java.net.SocketTimeoutException: connect timed out
Edit 2:
I've tried sending mail using Telnet using the exact same settings and that works. So apparently it's something with Keycloak or its underlying Java libraries that's causing issues sending e-mail.
Turns out that Keycloak works and that emails were blocked by the hosting provider.

Jenkins Pipeline script -> Send email

I have a Jenkins Pipeline project and want to send out an email.
I did set up the SMTP settings for Jenkins (Manage Jenkins -> Configure System -> E-mail Notification) and could successfully send a test mail by using the "Test Settings" button.
But if I use my Pipepline project with the following pipeline script:
post {
always {
emailext body: 'A Test EMail!3', recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: 'Test 999'
}
}
It fails with the following error:
Connection error sending email, retrying once more in 10 seconds...
Is there another place where I have to configure SMTP settings?
You need to configure email notification in the Configure Jenkins page

Client does not have permissions to send as this sender in Jenkins

I installed Jenkins (Jenkins ver. 2.46.2) and i have configured SMTP Server in Manage Jenkins -> Configure System -> Email Notification.
Test mail is works fine. But in Job email notification not working.
Error message as : Error sending to the following VALID addresses
I checked the Jenkins log file.
550 5.7.60 SMTP; Client does not have permissions to send as this sender [KL1PR03MB1749.apcprd03.prod.outlook.com]
DEBUG SMTP: got response code 550, with response: 550 5.7.60 SMTP; Client does not have permissions to send as this sender [KL1PR03MB1749.apcprd03.prod.outlook.com]
RSET
250 2.0.0 Resetting
com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.60 SMTP; Client does not have permissions to send as this sender [KL1PR03MB1749.apcprd03.prod.outlook.com]
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1862)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1100)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at hudson.plugins.emailext.ExtendedEmailPublisher.sendMail(ExtendedEmailPublisher.java:357)
at hudson.plugins.emailext.ExtendedEmailPublisher._perform(ExtendedEmailPublisher.java:322)
at hudson.plugins.emailext.ExtendedEmailPublisher.perform(ExtendedEmailPublisher.java:230)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:720)
at hudson.model.Build$BuildExecution.cleanUp(Build.java:196)
at hudson.model.Run.execute(Run.java:1775)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:405)
QUIT
How to fix this issue?
You need to set "System Admin email address" under Manage Jenkins -> Configure System. This is under the Jenkins Location header on that page!
Have a look at below thread.
How can I set the Sender's address in Jenkins?

email not sent in cronjob sugarCRM

I have set email settings using gmail from my localhost successfully and my sugar instance is sending test mail well. I also done with cron job setup and tested it.
The problem occurs when i try to send email in a cron job it cannot be send. My basic code i am using is
require_once('include/SugarPHPMailer.php');
$emailObj = new Email();
$defaults = $emailObj->getSystemDefaultEmail();
$mail = new SugarPHPMailer();
$mail->setMailerForSystem();
$mail->From = $defaults['email'];
$mail->FromName = $defaults['name'];
$mail->Subject=from_html($bean->name);
$mail->Body="hello this is testing ....!";
$mail->prepForOutbound();
$mail->AddAddress('abc#yahoo.com');
#$mail->Send();
The following errors displayed in fatal log .
09/24/12 10:58:07 [4560][1][FATAL] SMTP -> ERROR: EHLO not accepted from server. Code: , Reply:
09/24/12 10:58:07 [4560][1][FATAL] SMTP -> ERROR: HELO not accepted from server. Code: , Reply:
09/24/12 10:58:07 [4560][1][FATAL] SMTP -> ERROR:AUTH not accepted from server. Code: Reply:
09/24/12 10:58:07 [4560][1][FATAL] SugarPHPMailer encountered an error: SMTP Error: Could not authenticate.
09/24/12 10:58:07 [4560][1][FATAL] SugarPHPMailer encountered an error: SMTP Error: Could not connect to SMTP host.
09/24/12 10:58:07 [4560][1][FATAL] SugarPHPMailer encountered an error: An outgoing mail server is not configured to send emails. Please configure an outgoing mail server or select an outgoing mail server for the mail account that you are using in Settings >> Mail Account.
Some time it give follwing one,
09/24/12 11:01:07 [832][1][FATAL] SugarPHPMailer encountered an error: Language string failed to load: tls
can anybody help please?