logo

How to Send an Email via Naver in Julia 📂Julia

How to Send an Email via Naver in Julia

Overview

This document introduces how to send emails from Naver using the SMTPClient.jl package with SMTP 1. I use it to send reports to Kakao Mail when long-running simulations are finished, which helps to speed up my research.

20220717_232511.png

This way, knowing when simulations are finished without having to check the server myself, as Jordy notifies me via personal chat.

Code

Regardless of the programming language, the first thing to do is to enable SMTP as ‘Enabled’ in Naver Mail as shown below.

20220717_231516.png

Julia

using Dates

tic = now()
for t in 1:1000
  println(t)
end
toc = now()

using SMTPClient
opt = SendOptions(
  isSSL = true,
  username = "네이버아이디",
  passwd = "비밀번호")
#Provide the message body as RFC5322 within an IO
body = IOBuffer(
  "Date: ▷eq1◁tic\r\n" *
  "▷eq2◁(Dates.canonicalize(toc - tic))" *
  "\r\n")
url = "smtps://smtp.naver.com:465"
rcpt = ["<수신자@kakao.com>"]
from = "<발신자@naver.com>"
resp = send(url, rcpt, from, body, opt)

In the example above, the most critical part is url = "smtps://smtp.naver.com:465". This needs to be appropriately changed to whichever server you’re using, not just Naver. For the sending time, I fixed it to the moment of sending the mail using the now() from the Dates module, but if this does not match the actual clock, there might be a delay of about 10 minutes before the mail is sent.

Python

Before trying with Julia, I first attempted with Python. Strangely, even using SSL and setting the port to 456 did not work, but disabling SSL and switching to port 587 did the job. The following code, based on a blog2 that explained Google’s case, works well for Naver.

import smtplib
from email.mime.text import MIMEText

sendEmail = "발신자@naver.com"
recvEmail = "수신자@kakao.com"
password = "비밀번호"

smtpName = "smtp.naver.com" #smtp 서버 주소
smtpPort = 587 #smtp 포트 번호

text = "매일 내용"
msg = MIMEText(text) #MIMEText(text , _charset = "utf8")

msg['Subject'] = "시뮬레이션 종료"
msg['From'] = sendEmail
msg['To'] = recvEmail
print(msg.as_string())

s=smtplib.SMTP( smtpName , smtpPort ) #메일 서버 연결
s.starttls() #TLS 보안 처리
s.login( sendEmail , password ) #로그인
s.sendmail( sendEmail, recvEmail, msg.as_string() ) #메일 전송, 문자열로 변환하여 보냅니다.
s.close() #smtp 서버 연결을 종료합니다.

Environment

  • OS: Windows
  • julia: v1.7.0