229

I'm sending mail from Jenkins to an anonymous SMTP relay internally. That relay then securely sends mail to exchange online via TLS on port 587. The transport works perfectly, the issue is that Microsoft requires the Sender address match the authentication credentials login name which is the same as the account email address. For example sake, say this is foo@mycompany.com. How can I make jenkins always send mail from foo@mycompany.com?

BTW when I say the transport works perfectly what I mean specifically is that I can drop a formatted e-mail file into the "Pickup" directory on the SMTP relay server and it is properly sent to any recipient provided the From address is foo@mycompany.com.

CTOMarc
  • 2,329
  • 2
  • 12
  • 4
  • Does anyone know how to do this without using the UI? Maybe through the CLI or a groovy script or even a jenkins job? – jdf Aug 11 '15 at 21:09
  • Answered my own question about changing the admin email address without the UI below. – jdf Aug 11 '15 at 21:41

6 Answers6

367

Jenkins uses the System Admin e-mail address as the sender address for e-mail notification. You can configure this under Manage Jenkins -> Configure System. This is under the Jenkins Location header on that page! It is not immediately obvious that this setting is tied to the e-mail notification settings, since it is not under the E-mail notification header on the configuration page.

Friso
  • 3,711
  • 1
  • 10
  • 3
  • 1
    Using Ubuntu 12.04, local exim4 and Jenkins 1.565.1, installed from the external repo, I had to set this in order to make mail delivery work. – Max Hohenegger Aug 21 '14 at 08:39
106

Manage Jenkins -> Configure System -> Jenkins Location -> System Admin e-mail address

You can search the page for "Jenkins Location" to change the default value of address not configured yet to whatever you want.

Anshu Prateek
  • 2,861
  • 1
  • 16
  • 32
29

There are two places to add the "E-Mail Notificaitons"

  1. Global level
  2. Local level (Jenkins job level)

To set the E-Mail Notification at Global Level, follow below steps - 1) Go to Manage Jenkins 2) Configure System 3) Jenkins Location 4) System Admin e-mail address

enter image description here

To set the E-Mail Notification at local level (per Jenkins job level), follow below steps - 1) Click on the Job name (if existing one) 2) click on the "Configure" 3) Then look for the "Post-build Action" 4) Specify the email address under "E-mail Notification". 5) Save the changes.

enter image description here

Sanjeet Pandey
  • 431
  • 4
  • 23
  • 3
    On the local level it's not sender's address but recipient's. I guess it's not possible to configure sender's email address on a job level without writing custom scripts to send email. – insider Mar 13 '19 at 10:30
  • This doesn't answer the question. – Danijel Dec 02 '20 at 07:25
12

I'm not sure if it is what you meant, but Jenkins enables you to provide a full Sender E-mail Address for notifications. Go to Manage jenkins, then System configuration and find section called E-mail Notification.

You can provide you email address in a form of Jenkins <foo@mycompany.com>.

Łukasz Rżanek
  • 5,632
  • 24
  • 37
10

If you need to do this without using the jenkins UI (for instance in automating a jenkins setup) - you can do so with a groovy script.

import jenkins.model.*

def jenkinsLocationConfiguration = JenkinsLocationConfiguration.get()

jenkinsLocationConfiguration.setAdminAddress("[your admin name] <[your admin email address]>")
// example format -> .setAdminAddress("Jane Doe <foo@company_email.com>")    

jenkinsLocationConfiguration.save()

Do note: I did not write this script (although I have tested it and it works), all credit to Peter Halliday and his website with other helpful groovy scripts here.

mkobit
  • 34,772
  • 9
  • 135
  • 134
jdf
  • 670
  • 9
  • 20
0
import javax.mail.Message.RecipientType
import javax.mail.Address
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage

msg.setFrom(new InternetAddress("john...@server.com"))
David Buck
  • 3,439
  • 29
  • 24
  • 31
  • 2
    Code only answers are generally considered low quality posts. Can you please provide some explanation as to why your answer works? – Axe319 Oct 02 '20 at 15:28
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/27295825) – Trenton McKinney Oct 03 '20 at 04:00