728x90

에러 302 Redirect 후에 404, 500은 

Response.statuscode = 404, 500을 세팅해줘야함

 

세팅안해주면 statuscode = 200으로 나옴

728x90
public static string SHA256Hash(string text, string salt = "salt")
{
  HashAlgorithm algorithm = new SHA256Managed();

  byte[] textBytes = Encoding.UTF8.GetBytes(text);
  byte[] saltBytes = Encoding.UTF8.GetBytes(salt);

  //Combine salt and input text
  List<byte> listByte = new List<byte>();
  listByte.AddRange(textBytes);
  listByte.AddRange(saltBytes);

  byte[] hashedBytes = algorithm.ComputeHash(listByte.ToArray());
  StringBuilder strBuilder = new StringBuilder();
  foreach(byte b in hashedBytes)
  {
  strBuilder.AppendFormat("{0:x2}", b);
  }

  return strBuilder.ToString();
}

'IT > ASP.NET' 카테고리의 다른 글

에러처리  (0) 2022.07.01
[MVC5] Email Send (office365 메일 서버)  (0) 2021.08.08
[C#]SQL to Linq 변환 - "IN"절 사용  (0) 2021.01.11
[MVC5] json 길이 변경 설정 (최대값)  (0) 2020.12.23
728x90

View

<input type="button" value="send" onclick="fn_SendEmail();"/>

<script>

    function fn_SendEmail() {

        if (confirm("Are you sure?")) {

            $.ajax({
                type: "post",
                url: "SendEmail",
                dataType: "json",
                data: '',
                success: function (data) {
                    alert("메일을 전송했습니다.");
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("메일 전송에 실패했습니다.");
                    return false;
                }
            });
        }
    }

</script>

 

Controller

[HttpPost]
public JsonResult SendEmail()
{
    int rs = 0;

    string _host = "smtp.office365.com";
    int _port = 587; //기본 smtp port : 25 , office365 : 587
    string _userId = "userId"; //사용자 인증계정
    string _userPassword = "password"; //사용자 인증 비밀번호
    string fromEmail = "userId"; // 발송 메일 주소,  Office365는 인증계정과 발송메일주소가 동일해야함;
    string fromName = "Sanggeun"; // 발송인 정보
    string to = "csgct@naver.com"; //수신 메일 주소

    string cc = ""; //참조
    string subject = "이메일 제목";
    string body = "메일 본문입니다.";
    bool IsHtml = false; //body가 html일 때 true

    MailMessage message = SetMailMessage(fromEmail, fromName, to, cc, subject, body, IsHtml);
    Email email = new Email(_host, _port, _userId, _userPassword, message);
    email.Send();

    return Json(new { RS = rs });
}


public MailMessage SetMailMessage(string fromEmail, string fromName, string to, string cc, string subject, string body, bool IsHtml)
{
    MailMessage message = new MailMessage();

    MailAddress address = new MailAddress(fromEmail, fromName);

    message.From = address;
    message.To.Add(to);
    if (!string.IsNullOrEmpty(cc))
    {
        message.CC.Add(cc);
    }
    message.Subject = subject;
    message.Body = body;
    message.IsBodyHtml = IsHtml;

    return message;
}

 

Email.cs

public class Email
{
        public string _host;
        public int _port;
        public string _userId;
        public string _userPassword;
        public MailMessage _mailMassage;

        public Email() 
        {
            this._host = ConfigurationManager.AppSettings["SmtpHost"];
            this._port = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]);
            this._userId = string.IsNullOrEmpty(ConfigurationManager.AppSettings["SmtpUserId"]) ? "" : ConfigurationManager.AppSettings["SmtpUserId"];
            this._userPassword = string.IsNullOrEmpty(ConfigurationManager.AppSettings["SmtpUserPassword"]) ? "" : ConfigurationManager.AppSettings["SmtpUserPassword"];
        }


        public Email(string _host, int _port, string _userId, string _userPassword, MailMessage _mailMassage)
        {
            this._host = _host;
            this._port = _port;
            this._userId = _userId;
            this._userPassword = _userPassword;
            this._mailMassage = _mailMassage;
        }

        public void Send()
        {
            try
            {
                using (System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(_host, _port))
                {
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.EnableSsl = true; //ssl을 사용하여 암호화 할지 여부
                    smtp.Credentials = new System.Net.NetworkCredential(_userId, _userPassword);
                    smtp.Send(_mailMassage);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString(), ex.InnerException);
            }
            finally
            {
                _mailMassage.Dispose();
            }
        }
    }

 

'IT > ASP.NET' 카테고리의 다른 글

에러처리  (0) 2022.07.01
[C#] Compute SHA256 Hash with Salt  (0) 2021.11.01
[C#]SQL to Linq 변환 - "IN"절 사용  (0) 2021.01.11
[MVC5] json 길이 변경 설정 (최대값)  (0) 2020.12.23
728x90

SQL to Linq - "IN"절 사용

(SQL to Linq Convert - SQL “IN” clause)

 

Linq에서 SQL의 IN 쿼리(IN 절)은

 

아래와 같이 사용하면 됩니다.

 

public class ITEM
{
    public int SEQ { get; set; }
    public string ITEM_EVENT { get; set; }
    
    public ITEM(int seq, string itemEvent)
    {
        this.SEQ = seq;
        this.ITEM_EVENT = itemEvent;
    }
}

var eventList = new List<ITEM>();
eventList.Add(new ITEM(1, "01"));
eventList.Add(new ITEM(2, "02"));
eventList.Add(new ITEM(3, "01"));
eventList.Add(new ITEM(4, "03"));
eventList.Add(new ITEM(5, "02"));
eventList.Add(new ITEM(6, "04"));
eventList.Add(new ITEM(7, "05"));
eventList.Add(new ITEM(8, "06"));
eventList.Add(new ITEM(9, "01"));

/*
* 
* same expression
* WHERE IN ("01", "02", "03")
*
*/

string[] getEventList = { "01", "02", "03" };

var myEventList = 
( from list in eventList 
  where getEventList.Contains(list.ITEM_EVENT) 
  select list
).ToList();

 

'IT > ASP.NET' 카테고리의 다른 글

에러처리  (0) 2022.07.01
[C#] Compute SHA256 Hash with Salt  (0) 2021.11.01
[MVC5] Email Send (office365 메일 서버)  (0) 2021.08.08
[MVC5] json 길이 변경 설정 (최대값)  (0) 2020.12.23
728x90

Web.config 파일에 해당 설정 추가

<configuration>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483647" />
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

 

'IT > ASP.NET' 카테고리의 다른 글

에러처리  (0) 2022.07.01
[C#] Compute SHA256 Hash with Salt  (0) 2021.11.01
[MVC5] Email Send (office365 메일 서버)  (0) 2021.08.08
[C#]SQL to Linq 변환 - "IN"절 사용  (0) 2021.01.11

+ Recent posts